2
require 'aws-sigv4'

signer = Aws::Sigv4::Signer.new(access_key_id: access_key, region: 'us-east-1',secret_access_key: secret_key,service: 'execute-api')

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

 response = HTTParty.send(:get, 'https://sandbox.sellingpartnerapi-na.amazon.com/feeds/2021-06-30/feeds', 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'],
})

我收到以下错误

#<HTTParty::Response:0x55a044ec7c98 parsed_response={"errors"=>[{"message"=>"Access to requested resource is denied.", "code"=>"Unauthorized", "details"=>"Access token is missing in the request header."}]}, @response=#<Net::HTTPForbidden 403 Forbidden readbody=true>, @headers={"date"=>["Mon, 12 Jul 2021 09:16:40 GMT"], "content-type"=>["application/json"], "content-length"=>["187"], "connection"=>["close"], "x-amzn-requestid"=>["db0c65ea-f15a-4532-aadb-532b0eb1c6f2"], "x-amzn-errortype"=>["AccessDeniedException"], "x-amz-apigw-id"=>["CWZCzHploAMF6oA="]}>

当我尝试使用带有上面生成的签名的邮递员来点击请求时,我收到以下错误

{
    "errors": [
        {
            "message": "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.\n\nThe Canonical String for this request should have been\n'GET\n/feeds/2021-06-30/feeds\n\ncontent-type:\nhost:sandbox.sellingpartnerapi-na.amazon.com\nx-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\nx-amz-date:20210712T140633Z\n\ncontent-type;host;x-amz-content-sha256;x-amz-date\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'\n\nThe String-to-Sign should have been\n'AWS4-HMAC-SHA256\n20210712T140633Z\n20210712/us-east-1/execute-api/aws4_request\n5c4a3c5b6bfb8d42b8f45993ff0ba1fa49d82b1b182e6da616bd6ae5f7e98ffd'\n",
            "code": "InvalidSignature"
        }
    ]
}

这是屏幕截图或https://prnt.sc/1al04od

谁能帮我这个

4

1 回答 1

0

抱歉,这有点像一个死灵帖子,但我认为你(以及其他像我一样偶然发现这篇文章的人)应该得到一个答案。


所以有几件事:

首先:关于您的 Ruby 代码的初始错误,我的猜测是您遇到了一个常见问题,其中 Ruby Net::HTTPHeaderHTTParty以及其他 gem 使用)强制标头大写。

销售伙伴 API 强制执行x-amz-access-token. 因为Net::HTTPHeader该类将所有标头大写,最终请求实际上是发送类似X-Amz-Access-Token的内容,因此您会从 API 中看到“缺少访问令牌”错误。

有关更多详细信息和可能的解决方法,请参阅:https ://github.com/amzn/ sell-partner-api-docs/issues/292#issuecomment-759904882。

第二:我认为您不能只复制签名的请求标头并尝试重用它们。在构建请求时,您需要使用 Postman 的AWS Signature授权类型。

于 2021-09-03T15:08:28.210 回答