【问题标题】:httparty GET request: "Request is missing header: `Authorization`httparty GET 请求:“请求缺少标头:`Authorization`
【发布时间】:2018-07-12 14:55:28
【问题描述】:

我正在开发一个 Rails 应用程序,该应用程序向 Seamless.gov API 发出 GET 请求以获取表单的字段。当我运行以下 curl 命令时,我能够得到 JSON 响应。但是,当我从 rails 执行此操作时,会出现错误:

{ "error": true, "error_log": [ { "error_code": "missing_header", "error_message": "请求缺少标头:Authorization", “错误描述”: "{\"Host\":\"nycopp.seamlessdocs.com\",\"Connection\":\"close\",\"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

传递的标头是否错误?

【问题讨论】:

    标签: ruby http-headers unix-timestamp httparty ruby-on-rails-5.2


    【解决方案1】:

    这解决了问题:

    self.class.get(relative_uri, headers: generate_headers)

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2018-04-06
      • 2019-06-23
      • 2015-11-08
      相关资源
      最近更新 更多