2010-11-23 87 views
1

我是使用網站API的新手。但是很長一段時間我想學習這個,今天開始用一個簡單的例子來說明如何從soundcloud獲取信息。下面是簡單的例子,從他們website如何獲得連接到soundcloud api的「oauth訪問祕密」

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::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

這個簡單的例子,怎能不?

回答

1

與OAuth一樣,如果您希望最終用戶通過您的應用程序訪問Soundcloud的受保護的資源,您將不得不使用Soundcloud註冊您的應用程序。

當您使用OAuth從Soundcloud請求access_token時,它會返回您並access_tokenoauth_token_secret。那oauth_token_secret就是你提到的_YOUR_OAUTH_ACCESS_SECRET_

我不知道你對OAuth有多熟悉。該文檔可以找到here


編輯 OAuth授權方案改變了一段時間後,(例如獲得訪問令牌需要指定一個oauth_verifier)。

請參閱SoundCloud example on Authentication使用最新的OAuth規範。

+0

感謝您的快速答覆。但我認爲你不明白我的問題是什麼,也許是因爲它太簡單了。爲了請求access_token(access_token = OAuth :: AccessToken.new(sc_consumer,'YOUR_OAUTH_ACCESS_TOKEN','YOUR_OAUTH_ACCESS_SECRET'))我已經必須知道** YOUR_OAUTH_ACCESS_SECRET **你明白我的意思了嗎? – dedan 2010-11-23 16:26:22

+0

@dedan,對於你的問題,答案是肯定的,但這似乎是一箇舊的OAuth身份驗證方案。查看我編輯過的帖子以獲取更多 – 2010-11-23 16:32:30

2

問題的答案很簡單。我的問題是我有 在soundcloud生產系統 soundcloud.com上註冊了我的應用程序,但是指示我的請求針對sandbox-soundcloud.com。

我不得不去sandbox-soundcloud.com,註冊一個新的用戶帳戶,並創建一個新的客戶端應用程序,並且一切正常。

沙箱的更多信息,請訪問: http://github.com/soundcloud/api/wiki/Appendix-B-Sandbox

相關問題