当我使用httparty发出GET请求时,我当前收到了一个错误。当我使用curl
时,调用可以正常工作。错误如下:
\"Authdate\":\"1531403501\"}“},{ "error_code":"external_auth_error","error_message":”日期头丢失或时间戳越界“}]}
当我通过curl
发出请求时,这是我使用的头文件。
curl -X GET -H "AuthDate: 1531403501"
但是,正如您所看到的,请求从AuthDate
更改为导致错误的Authdate
。下面是我打电话的方式:
require 'openssl'
require 'base64'
module SeamlessGov
class Form
include HTTParty
attr_accessor :form_id
base_uri "https://nycopp.seamlessdocs.com/api"
def initialize(id)
@api_key = ENV['SEAMLESS_GOV_API_KEY']
@signature = generate_signature
@form_id = id
@timestamp = Time.now.to_i
end
def relative_uri
"/form/#{@form_id}/elements"
end
def create_form
self.class.get(relative_uri, headers: generate_headers)
end
private
def generate_signature
OpenSSL::HMAC.hexdigest('sha256', ENV['SEAMLESS_GOV_SECRET'], "GET+#{relative_uri}+#{@timestamp}")
end
def generate_headers
{
"Authorization" => "HMAC-SHA256 api_key='#{@api_key}' signature='#{@signature}'",
"AuthDate" => @timestamp
}
end
end
end
有什么解决方法吗?
发布于 2018-07-13 04:27:11
根据规范https://stackoverflow.com/a/41169947/1518336,Header是不区分大小写的,所以看起来您访问的服务器是错误的。
看看在其上实现HTTParty的Net::HTTPHeader
与原始散列访问不同,HTTPHeader通过不区分大小写的键提供访问
它看起来像是downcases类统一的头关键字。
您可能需要查看另一个不依赖于net/http
的网络库。也许是curb
发布于 2018-07-13 05:19:57
在下面的文章中有一个解决方案
https://github.com/jnunemaker/httparty/issues/406#issuecomment-239542015
我创建了文件lib/net_http.rb
require 'net/http'
class Net::HTTP::ImmutableHeaderKey
attr_reader :key
def initialize(key)
@key = key
end
def downcase
self
end
def capitalize
self
end
def split(*)
[self]
end
def hash
key.hash
end
def eql?(other)
key.eql? other.key.eql?
end
def to_s
def self.to_s
key
end
self
end
end
然后在头文件中
def generate_headers
{
"Authorization" => "HMAC-SHA256 api_key='#{@api_key}' signature='#{@timestamp}'",
Net::HTTP::ImmutableHeaderKey.new('AuthDate') => "#{@timestamp}"
}
end
https://stackoverflow.com/questions/51312102
复制相似问题