【发布时间】: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
到目前为止,我一直在尝试使用找到的一些代码手动签署请求 here 和 there 没有成功。我的理解是签名必须是请求本身的一种指纹,但我在概念上并不确定它,更不用说如何在技术上实现它。
问题: 如果我的请求类似于 (?):
,我的 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 和this post 的末尾添加'&',但没有成功。
请赐教!
【问题讨论】:
标签: ruby-on-rails authentication oauth cryptography api-design