English 中文(简体)
How to get the "oauth access secret" for a connection to the soundcloud api
原标题:

I am new to using APIs of Websites. But since a long time I wanted to learn this and today started with the simple example of how to access information from soundcloud. Here is the code of the simple example from their website

require  rubygems 
gem  soundcloud-ruby-api-wrapper 
require  soundcloud 

gem  oauth 
require  oauth 


# Create a Soundcloud OAuth consumer token object
sc_consumer = Soundcloud.consumer( YOUR_APPLICATION_CONSUMER_TOKEN , YOUR_APPLICATION_CONSUMER_SECRET )

# Create an OAuth access token object
access_token = OAuth::AccessToken.new(sc_consumer,  YOUR_OAUTH_ACCESS_TOKEN ,  YOUR_OAUTH_ACCESS_SECRET )

# Create an authenticated Soundcloud client, based on the access token
sc_client = Soundcloud.register({:access_token => access_token})

# Get the logged in user 
my_user = sc_client.User.find_me

# Display his full name
p "Hello, my name is #{my_user.full_name}"

I know what to set as:

  • YOUR_APPLICATION_CONSUMER_TOKEN
  • YOUR_APPLICATION_CONSUMER_SECRET

as this was given when registering a application on soundcloud.

I set the YOUR_OAUTH_ACCESS_TOKEN to http://api.soundcloud.com/oauth/access_token which was also written on the soundcloud site, but I have no idea where to get the

_YOUR_OAUTH_ACCESS_SECRET_ from.

Is this access secret also a random string that I get from somewhere, do I have to generate it by myself.


EDIT As suggested in the answer of the Elite Gentlemen I also tried the Soundcloud example on authentication. I post here the piece of code which already leads to the error:

require  rubygems 
gem  soundcloud-ruby-api-wrapper 
require  soundcloud 

# oAuth setup code:  
# Enter your consumer key and consumer secret values here:
@consumer_application = {:key =>  QrhxUWqgIswl8a9ESYw , :secret =>  tqsUGUD3PscK17G2KCQ4lRzilA2K5L5q2BFjArJzmjc }

# Enter the path to your audio file here.  
path_to_audio_file = "your/absolute/path/to/audio_file.ext"

# Set up an oAuth consumer.  
@consumer = OAuth::Consumer.new @consumer_application[:key], @consumer_application[:secret], 
{
  :site               =>  http://api.sandbox-soundcloud.com , 
  :request_token_path =>  /oauth/request_token ,
  :access_token_path  =>  /oauth/access_token ,
  :authorize_path     =>  /oauth/authorize 
}

# Obtain an oAuth request token
puts "Get request token"
request_token = @consumer.get_request_token

The error message I receive then is:

OAuth::Unauthorized: 401 Unauthorized

method token_request in consumer.rb at line 217 method get_request_token in consumer.rb at line 139 at top level in test1.rb at line 25

How can this simple example fail?

问题回答

The answer to the question is very simple. My problem was that I had registered my application on the soundcloud production system soundcloud.com but directed my requests against sandbox-soundcloud.com.

I had to go to sandbox-soundcloud.com, register a new user account and make a new client application and everything worked perfectly.

More information on the Sandbox is available here: http://github.com/soundcloud/api/wiki/Appendix-B-Sandbox

As with OAuth, you will have to register your application with Soundcloud if you want the end-user to access Soundcloud s protected resources through your application.

When you request an access_token from Soundcloud using OAuth, it will return you and access_token and a oauth_token_secret. That oauth_token_secret is what you mentioned as YOUR_OAUTH_ACCESS_SECRET

I don t know how familiar you are with OAuth. The documentation can be found here.


Edit OAuth authorization scheme changed a while back, (e.g. getting an access token requires you to specify a oauth_verifier).

See the SoundCloud example on Authentication using the latest OAuth specification.





相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

getting XML from other domain using ASP.NET

I m fairly new to ASP.NET. And I was wondering how I could go about getting xml from a site (Kuler s API in this case), and then post the result using AJAX? So what I want here, is to be able to do a ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

UML Diagram to Model API

I need to create a diagram to document a RESTFul API that build, which UML diagram should I use? Thanks in advance,

How best to expose Rails methods via an API?

Let s say I have a model foo, and my model has a publish! method that changes a few properties on that model and potentially a few others too. Now, the Rails way suggests that I expose my model over ...

简讯

我是否可以使用某些软件来建立简便的航天国家服务器,最好是在 Java? 所有我都希望我的航天国家服务机在任何要求中都用同样的IP地址来回答。

About paypal express checkout api

In this picture,there are 3 main steps:SetExpressCheckout,GetExpressCheckoutDetails and DoExpressCheckoutDetails,I m now sure SetExpressCheckout is to be called by myself,what about ...

热门标签