【发布时间】:2021-03-21 18:46:11
【问题描述】:
我需要通过重力表单 API(作为插件安装在 WordPress 服务器中)检索一些数据。我必须实施 OAuth 1.0。一些可用于此类身份验证的 ruby gem 对我来说效果不佳,因此我决定自己实现它。
如果我不添加额外的参数,我可以对基本字符串进行签名,但是一旦添加它们,我就会收到错误 Invalid signature - provided signature does not match.
我的代码如下所示:
# This OAUTH implementation works without extra query params.
require 'httparty'
OAUTH_TOKEN = "API customer secret token"
OAUTH_PASSWORD = "API customer key token"
url = 'https://localhost/wp-json/gf/v2/entries'
params = "oauth_consumer_key=#{OAUTH_TOKEN}&oauth_nonce=#{SecureRandom.hex}&oauth_signature_method=HMAC-SHA1&oauth_timestamp=#{Time.now.to_i}&oauth_token=#{OAUTH_PASSWORD}&oauth_version=1.0"
base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(params)
secret = "#{OAUTH_PASSWORD}&#{}"
oauth_signature = sign(secret, base_string)
testable_url = url + '?' + params + '&oauth_signature=' + oauth_signature
response = HTTParty.get(testable_url)
def sign( secret, base_string )
CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', secret, base_string)}").chomp)
end
但是一旦我添加了我需要的查询参数,它就会崩溃。
query_params = 'search={"field_filters":[{"key":"'+form['key']+'","value":"'+email+'","operator":"is"},{"key":"source_url","value":"'+form['source_url'].to_s+'","operator":"is"}]}'
我正在更新 base_string 以包含 query_params 然后我再次签名并更新最终 url
base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(query_params) + '&' + CGI.escape(params)
oauth_signature = sign(secret, base_string)
testable_url = url + '?' + query_params + '&' + params + '&oauth_signature=' + oauth_signature
结果我得到以下网址
"https://localhost/wp-json/gf/v2/entries?search={\"field_filters\":[{\"key\":\"2\",\"value\":\"some@email.com\",\"operator\":\"is\"},{\"key\":\"source_url\",\"value\":\"https://localhost/some_url_i_need/\",\"operator\":\"is\"}]}&oauth_consumer_key=APICUSTOMERSECRET&oauth_nonce=81a6ce4dcabd5ec1059f66ca7ae727e3&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1607594914&oauth_token=APICUSTOMER_TOKEN&oauth_version=1.0&oauth_signature=LIZpR27KNHsw3iWArJjSs%2FsvIek%3D"
由于Invalid signature - provided signature does not match. 错误,这不起作用。
按照文档和我读过的其他帖子 (https://stackoverflow.com/a/4789210) 中的建议,我已经对参数进行了排序(基本上我在基本字符串的末尾添加了我需要的 search 参数,但这并没有解决我的问题。
注意:它在 Postman 中工作,Postman 生成的 URL 与我使用 Ruby 得到的 URL 非常相似。
【问题讨论】:
-
你能粘贴邮递员生成的网址吗?
-
"https://localhost/wp-json/gf/v2/entries?search={\"field_filters\": [{\"key\":\"2\",\"value\”:\”some@email.com,\”operator\":\"is\"}, {\"key\":\"source_url\",\"value\":\"https://source_example.com/\",\"operator\":\"is\"}]}&oauth_consumer_key=CONSUMERKEY&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1607771258&oauth_nonce=Nw4R2icbtxi&oauth_version=1.0&oauth_signature=7wR/c0PMJlX/GVac76A5vkJM+Z8="
标签: ruby-on-rails ruby oauth gravity-forms-plugin