【问题标题】:Yahoo Oauth in Ruby API Request - Signature InvalidRuby API 请求中的 Yahoo Oauth - 签名无效
【发布时间】:2014-10-10 21:28:14
【问题描述】:

我已经成功获得了访问令牌和访问密码。现在我正在尝试使用 OAuth 信息发出 API 请求。

我正在关注 yahoo 文档(不是很有帮助): 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”。

我做错了什么?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby api oauth yahoo-api


    【解决方案1】:
        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 
    

    参数必须按字母顺序排列!此外,密钥是 yahoo 消费者密钥加上最终的 oauth 密钥!

    【讨论】:

      【解决方案2】:

      我认为有问题的第一件事是 parameters 有很多你不想要的空白。请尝试以下方法:

      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)
      

      【讨论】:

      • 嘿 Ronen,感谢您的回答。从字面上看,我一个小时前才弄清楚。是的,我在 SO 上显示它的方式并不是我编写实际代码的方式——这样做只是为了便于阅读。
      • 我们刚刚相撞,所以再看看我修改后的答案。 &符号不应添加到您的密钥后缀。
      • 再次感谢您的帮助!我将其用作模型,它似乎可以工作 gist.github.com/cheenu/1469815,尽管此模型在参数中省略了 oauth 令牌 - 我添加了一些东西并且它工作了。
      猜你喜欢
      • 2015-11-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多