[英]Yahoo Oauth in Ruby API Request - Signature Invalid
我已經成功獲得了訪問令牌和訪問權限。 現在我正在嘗試使用OAuth信息發出API請求。
我跟隨雅虎文檔(不是很有幫助): https : //developer.yahoo.com/oauth/guide/oauth-make-request.html https://developer.yahoo.com/oauth/guide/oauth -signing.html
另外,我試圖密切關注這個例子: https : //gist.github.com/cheenu/1469815
這是代碼:(為方便起見我拆分了長網址)
require 'cgi'
require 'base64'
require 'openssl'
url = "http://fantasysports.yahooapis.com/fantasy/v2/game/nfl"
parameters = "format=json
&realm=yahooapis.com
&oauth_consumer_key=#{Rails.application.secrets.yhoo_consumer_key}
&oauth_nonce=#{SecureRandom.hex}
&oauth_signature_method=HMAC-SHA1
&oauth_timestamp=#{Time.now.to_i}
&oauth_token=#{ApiVar.final_oauth_token} #the access token
&oauth_version=1.0"
base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(parameters)
oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', ApiVar.final_oauth_secret + "&", base_string)}").chomp)
#ApiVar.final_oauth_secret is the access token secret - is that what I should be putting there?
testable_url = url + '?' + parameters + '&oauth_signature=' + oauth_signature
p testable_url
response = HTTParty.get(testable_url)
我的回復給了我“signature_invalid”。
我究竟做錯了什么?
謝謝!
url = "http://fantasysports.yahooapis.com/fantasy/v2/league/{league-key}/players"
parameters = "format=json&oauth_consumer_key=#{Rails.application.secrets.yhoo_consumer_key}&oauth_nonce=#{SecureRandom.hex}&oauth_signature_method=HMAC-SHA1&oauth_timestamp=#{Time.now.to_i}&oauth_token=#{ApiVar.final_oauth_token}&oauth_version=1.0&realm=yahooapis.com"
base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(parameters)
secret = "#{Rails.application.secrets.yhoo_consumer_secret}&#{ApiVar.final_oauth_secret}"
oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', secret, base_string)}").chomp)
testable_url = url + '?' + parameters + '&oauth_signature=' + oauth_signature
p testable_url
response = HTTParty.get(testable_url)
#{Rails.application.secrets.yhoo_consumer_secret}&#{ApiVar.final_oauth_secret}" - correct secret key
參數必須按字母順序排序! 此外,秘密密鑰是雅虎消費者秘密加上最后的oauth秘密!
我可以看到第一件有問題的事情是, 參數有很多你不想要的空白。 請嘗試以下方法:
parameters = "format=json" +
"&realm=yahooapis.com" +
"&oauth_consumer_key=#{Rails.application.secrets.yhoo_consumer_key}" +
"&oauth_nonce=#{SecureRandom.hex}" +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=#{Time.now.to_i}" +
"&oauth_token=#{ApiVar.final_oauth_token}" +
"&oauth_version=1.0"
另一個問題是,當您創建簽名時,我不相信您的密鑰需要添加&符號:
oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', ApiVar.final_oauth_secret, base_string)}").chomp)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.