【问题标题】:Rails — Signing OAuth1 requestsRails — 签署 OAuth1 请求
【发布时间】:2019-03-25 01:49:47
【问题描述】:

我目前正在尝试实现一个 api (http://developers.music-story.com),其身份验证使用 OAuth 1.0 技术(请求也已签名)。当我创建我的开发帐户时,他们为我提供了 4 个不同的密钥,例如:

oauth_consummer_key = some_hexa_str_long_of_40_chars
consummer_secret = some_other_hexa_str_long_of_40_chars
oauth_access_token = some_other_hexa_str_long_of_40_chars
oauth_token_secret = some_other_hexa_str_long_of_40_chars

到目前为止,我一直在尝试使用找到的一些代码手动签署请求 herethere 没有成功。我的理解是签名必须是请求本身的一种指纹,但我在概念上并不确定它,更不用说如何在技术上实现它。

问题: 如果我的请求类似于 (?):

,我的 OAuth 1 签名 会是什么?
HTTParty.get("http://api.music-story.com/en/show/search?
oauth_signature=I_DONT_KNOW_HOW_TO_GET_THIS
&oauth_token=I_HAVE_THIS_ONE_ALREADY
&name=whatever")

Edit1:这是我迄今为止尝试过的并引发(无效的 oauth 密钥消息)api 响应:

oauth_consumer_key = oauth_consummer_key
oauth_nonce = Random.rand(100000).to_s
oauth_signature_method = 'HMAC-SHA1'
oauth_timestamp = Time.now.to_i.to_s
oauth_version = '1.0'

url = "http://api.music-story.com/en/artist/search?"

parameters = 'oauth_consumer_key=' +
              oauth_consumer_key +
              '&oauth_nonce=' +
              oauth_nonce +
              '&oauth_signature_method=' +
              oauth_signature_method +
              '&oauth_timestamp=' +
              oauth_timestamp +
              '&oauth_version=' +
              oauth_version


base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(parameters) + '&name=whatever'
secret_key = oauth_token_secret
oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1',secret_key, base_string)}").chomp)

oauth_token = oauth_access_token

response = HTTParty.get("http://api.music-story.com/en/artist/search?name=someartistname&oauth_signature=#{oauth_signature}&oauth_token=#{oauth_token}")
puts JSON.parse(response.to_json)
# {"root"=>{"version"=>"1.29", "code"=>"-3", "error"=>{"type"=>"OAuthException", "message"=>"Incorrect oauth_signature", "errorcode"=>"40107"}}}

Edit2我还尝试在oauth_tokenthis post 的末尾添加'&',但没有成功。

请赐教!

【问题讨论】:

    标签: ruby-on-rails authentication oauth cryptography api-design


    【解决方案1】:

    在我的情况下,问题是在 url 方案的开头有 http://。 用 api.music-story.com... 之类的东西替换查询的 url 参数对我有用

    【讨论】:

      【解决方案2】:

      您可以使用Ruby Oauth。这是一个示例

      consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {site: "http://api.music-story.com"})
      access_token = OAuth::AccessToken.new(consumer, token=oauth_access_token, secret=oauth_token_secret )
      response = access_token.get("/en/artist/search?name=someartistname")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        • 2011-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-24
        • 2021-06-03
        相关资源
        最近更新 更多