向销售合作伙伴 API 沙盒端点发送请求返回 InvalidSignature

问题描述

我目前正在尝试使用 ruby​​ 和 HTTP.rb gem 创建文档并将其上传到 SP-API 沙箱环境。我的步骤是:

  1. 通过刷新令牌请求 LWA 访问令牌
  2. 承担角色并请求 STS 令牌
  3. 使用 AWS::SignV4 SDK 签署请求标头
  4. 将 POST 请求发送到端点 /Feeds/2020-09-04/documents,正文为 json: { 'contentType' => 'text/tab-separated-values; charset=UTF-8' }

但是,SP-API 不断向我返回 "code": "InvalidSignature"。但是我所有的其他“GET”请求,如 get_orders、get_order_items 都正常工作。
以下是我发送请求的方式:


@url = '/Feeds/2020-09-04/documents'
@body = if sandBox
    { 'contentType' => 'text/tab-separated-values; charset=UTF-8' }
 else
    { 'contentType' => 'text/xml; charset=UTF-8' }
 end
 @request_type = 'POST' 

response = http.headers(headers).send(@request_type.downcase.to_sym,request_url,json: @body)

解决方法

我检查了 AWS::Signer::V4 文档,结果我也应该将正文传递给签名者。
signer.sign_request(http_method: @request_type,url: request_url,body: @body)

,

我发布了执行此操作的 amz_sp_api ruby​​gem,但我欢迎贡献用于加密 SP-API 要求的提要提交的 ruby​​ 代码:https://github.com/ericcj/amz_sp_api/issues/1