我正在开发一个Rails应用程序,该应用程序向Seamless.gov API发出GET请求以获取表单字段。当我运行以下curl
命令时,我可以执行JSON响应。但是,当我从rails上执行操作时,会出现错误:
{“ error”:true,“ error_log”:[{“ error_code”:“ missing_header”, “ error_message”:“请求缺少标头:
Authorization
”, “错误说明”: “ {\”主机\“:\” nycopp.seamlessdocs.com \“,\”连接\“:\”关闭\“,\” X-
这是我创建的代码:
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, 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
在控制器中,我以如下形式传递id:
def display_form
@form = SeamlessGov::Form.new(ENV['FORM_ID']).create_form
end
标题是否传递错误?
答案 0 :(得分:1)
这解决了问题:
self.class.get(relative_uri, headers: generate_headers)