首页 > 解决方案 > Rails — 签署 OAuth1 请求

问题描述

我目前正在尝试实现一个 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

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

问题:如果我的请求类似于(?), 我的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_token的末尾和解决方案的末尾添加“&”,但没有成功。

请赐教!

标签: ruby-on-railsauthenticationoauthcryptographyapi-design

解决方案


就我而言,问题http://出在 url 方案的开头。api.music-story.com...用对我有用的东西替换查询的 url 参数


推荐阅读