gpt4 book ai didi

ruby - Amazon Selling Partner API - 签名请求(ruby 实现)

转载 作者:行者123 更新时间:2023-12-04 17:21:52 50 4
gpt4 key购买 nike

根据亚马逊销售合作伙伴 API 文档,我能够获得 LWA 访问 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 访问 token ,但我不知道发生了什么,因为这是一个新的 API,并且没有太多实现,尤其是在 ruby​​ 中。

谁能指点一下?

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

Aws::Sigv4::Signer

最佳答案

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

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

Net::HTTPHeader 在发送请求之前将所有请求 header 大写,并且 x-amz-access-token header 区分大小写。

如果您使用有效值填充 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

关于ruby - Amazon Selling Partner API - 签名请求(ruby 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65669679/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com