2
我試圖通過oauth連接到雅虎。我用紅寶石使用oauth寶石。如何使用Ruby連接到使用oauth的Yahoo?
我可以成功獲取訪問令牌,但是我無法使用它來進行任何調用。這裏是我的代碼:
def oauth
OAuth::Consumer.new(ApplicationConfig['yahoo']['access_token'],
ApplicationConfig['yahoo']['secret'],
{
:site => 'https://api.login.yahoo.com',
:scheme => :query_string,
:http_method => :get,
:request_token_path => '/oauth/v2/get_request_token',
:access_token_path => '/oauth/v2/get_token',
:authorize_path => '/oauth/v2/request_auth',
:oauth_callback => 'http://my_callback'
})
end
def oauth_api
OAuth::Consumer.new(ApplicationConfig['yahoo']['access_token'],
ApplicationConfig['yahoo']['secret'],
{
:site => 'http://address.yahooapis.com',
:scheme => :header,
:realm => 'yahooapis.com',
:http_method => :get,
:request_token_path => '/oauth/v2/get_request_token',
:access_token_path => '/oauth/v2/get_token',
:authorize_path => '/oauth/v2/request_auth'
})
end
@request_token = oauth.get_request_token({ :oauth_callback => 'my_callback' })
# go to @request_token.authorize_url
@access_token = @request_token.get_access_token({ :oauth_verifier => params[:oauth_verifier] })
@access_token.consumer = oauth_api
## Now I have an access token, but if I do something like:
response = @access_token.get('/v1/searchContacts')
## Then I get
#<Net::HTTPForbidden 403 Forbidden readbody=true>
# >> response.body
# => "<!-- web231.address.pim.re3.yahoo.com uncompressed/chunked Wed Jan 13 01:15:46 PST 2010 -->\n"
基本上我會希望工作。
我還沒找到任何有關如何使用Ruby連接到雅虎的示例。我希望其他人已經做到了,他/她願意提供幫助。