开发者

How to get the "oauth access secret" for a connection to the soundcloud api

开发者 https://www.devze.com 2023-01-26 18:40 出处:网络
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

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 开发者_Go百科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.

0

精彩评论

暂无评论...
验证码 换一张
取 消