亚马逊销售合作伙伴 API - 签名请求(ruby 实施)

Amazon Selling Partner API - signed request (ruby implementation)

根据亚马逊销售合作伙伴 API 文档,我获得了 LWA access token。 但是,我无法向 REST API.

发出请求

https://github.com/amzn/selling-partner-api-docs/blob/main/guides/developer-guide/SellingPartnerApiDeveloperGuide.md#connecting-to-the-selling-partner-api

我尝试使用 aws-sdk-signer 创建签名请求

access_token = 'LWA access token'

signer = Aws::Sigv4::Signer.new(
  access_key_id: 'my access id',
  region: 'us-east-1',
  secret_access_key: 'my access key,
  service: 'execute-api',
)

signature = signer.sign_request(
  http_method: 'GET',
  url: 'https://sellingpartnerapi-na.amazon.com/orders/v0/orders',
  headers: {
    'host' => 'sellingpartnerapi-na.amazon.com',
    'user_agent' => 'test (Language=Ruby)',
    'x-amz-access-token' => access_token
  })

response = HTTParty.send(:get, 'https://sellingpartnerapi-na.amazon.com/orders/v0/orders', headers: {
  'host' => signature.headers['host'],
  'user_agent' => 'test (Language=Ruby)',
  'x-amz-access-token' => access_token,
  'x-amz-content-sha256' => signature.headers['x-amz-content-sha256'],
  'x-amz-date' => signature.headers['x-amz-date'],
  'Authorization' => signature.headers['authorization'],
})

回应

{"errors"=>[{"message"=>"Access to requested resource is denied.", "code"=>"Unauthorized", "details"=>"Access token is missing in the request header."}]}

看起来我没有正确签署 LWA 访问令牌,但我不知道发生了什么,因为这是一个新的 API 并且没有太多实现,尤其是在 ruby 中。

有人能指点一下吗?

更新:我关注了歌手文档 https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/Sigv4/Signer.html

Aws::Sigv4::Signer

“请求 header 中缺少访问令牌”听起来您的 x-amz-access-token 有问题。你是这样捡的吗? https://github.com/ericcj/amz_sp_api/blob/main/lib/sp_api_client.rb#L40

对于遇到此问题的任何人:

您的问题可能源于 HTTParty(或其他 HTTP 客户端 gem)在幕后使用 Ruby 的 Net::HTTPHeader

Net::HTTPHeader在发送请求之前将所有请求header大写并且x-amz-access-tokenheader是case-sensitive.

如果您使用有效值填充 x-amz-access-token,但仍然收到以下错误:

{
    "message": "Access to requested resource is denied.",
    "code": "Unauthorized",
    "details": "Access token is missing in the request header."
}

...那么您可能 运行 遇到了这个问题。

你可以像这样重载 Net:HTTPHeader.capitalize 来绕过这个:

module Net::HTTPHeader
  def capitalize(name)
    name
  end
  private :capitalize
end

另请参阅:https://github.com/amzn/selling-partner-api-docs/issues/292#issuecomment-759904882