如何获取“oauth 访问秘密”用于连接到 soundcloud api
我刚开始使用网站 API。但长期以来我一直想学习这一点,今天从如何从 soundcloud 访问信息的简单示例开始。 的简单示例的代码
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}"
这是来自他们的 网站 知道要设置的内容:
- “YOUR_APPLICATION_CONSUMER_TOKEN”
- “YOUR_APPLICATION_CONSUMER_SECRET”
,因为这是在 soundcloud 上注册应用程序时给出的。
我将“YOUR_OAUTH_ACCESS_TOKEN”设置为 http://api.soundcloud.com/oauth/access_token 这也写在 soundcloud 网站上,但我不知道从哪里获取
_YOUR_OAUTH_ACCESS_SECRET_。
这个访问秘密也是我从某处获得的随机字符串吗?我必须自己生成它吗?
编辑 正如精英绅士的回答中所建议的,我还尝试了有关身份验证的 Soundcloud 示例。我在这里发布了已经导致错误的代码:
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
然后我收到的错误消息是:
OAuth::未经授权:401 未经授权
consumer.rb 中的方法 token_request 第217行方法中的get_request_token Consumer.rb 位于顶部第 139 行 test1.rb 第 25 行的级别
这个简单的示例怎么会失败呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题的答案很简单。我的问题是我有
在 soundcloud 制作系统上注册我的应用程序
soundcloud.com 但将我的请求定向到 sandbox-soundcloud.com。
我必须去 sandbox-soundcloud.com,注册一个新的用户帐户并创建一个
新的客户端应用程序一切正常。
有关沙箱的更多信息,请访问:
http://github.com/soundcloud/api/wiki/Appendix-B-Sandbox
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
与 OAuth 一样,如果您希望最终用户通过您的应用程序访问 Soundcloud 的受保护资源,则必须向 Soundcloud 注册您的应用程序。
当您使用 OAuth 向 Soundcloud 请求
access_token
时,它将返回您和access_token
以及oauth_token_secret
。oauth_token_secret
就是您提到的 YOUR_OAUTH_ACCESS_SECRET我不知道您对 OAuth 有多熟悉。该文档可以在此处找到。
编辑 OAuth 授权方案不久前发生了更改(例如,获取访问令牌需要您指定
oauth_verifier
)。请参阅使用最新 OAuth 规范的SoundCloud 身份验证示例。
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 andaccess_token
and aoauth_token_secret
. Thatoauth_token_secret
is what you mentioned as YOUR_OAUTH_ACCESS_SECRETI 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.