“AWS4签名请求签名不匹配”

2024-04-29 14:47:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我按照here中的步骤为aws4sdk生成规范字符串和要签名的字符串。我得到了403。这是我打印response.text时得到的:

{\"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.

The Canonical String for this request should have been
\\'PUT
/dev/trial

content-type:application/x-www-form-urlencoded
host:myendpoint.execute-api.us-west-2.amazonaws.com
x-amz-date:20190918T002703Z

content-type;host;x-amz-date
402d04afaaf71664b4820123456789bda0df4601423fe13cc851b475798016b5\\'

The String-to-Sign should have been
\\'AWS4-HMAC-SHA256
20190918T002703Z
20190918/us-west-2/execute-api/aws4_request
55f919eb5d745c06760eea01da0123456789b3b1ac1cf2bf0627701d06db0780\\'
\"}

我还尝试打印规范字符串和我计算的要签名的字符串,它们如下所示:

实际规范请求:

PUT
/dev/trial

content-type:application/x-www-form-urlencoded
host:myendpoint.execute-api.us-west-2.amazonaws.com
x-amz-date:20190918T002703Z

content-type;host;x-amz-date
402d04afaaf71664b4820123456789bda0df4601423fe13cc851b475798016b5

要签名的实际字符串:

AWS4-HMAC-SHA256
20190918T002703Z
20190918/us-west-2/execute-api/aws4_request
55f919eb5d745c06760eea01da0123456789b3b1ac1cf2bf0627701d06db0780

至少对我来说,AWS的计算结果和我的计算结果完全相同。那么我还是得到了“签名不匹配”的错误吗

我确实看过类似的问题。他们都对请求有一些问题,例如缺少\n或不同的日期格式。我不相信我有这些问题

如果有帮助的话,下面是我如何创建签名的一些组件:

canonical_request = method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash

algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + service + '/' + 'aws4_request'
string_to_sign = algorithm + '\n' + amzdate + '\n' + credential_scope + '\n' + hashlib.sha256(
    canonical_request.encode('utf-8')).hexdigest()

authorization_header = algorithm + ' ' + 'Credential=' + access_key + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature

headers = {'x-amz-date': amzdate, 'Authorization': authorization_header, 'host': host,
'content-type': content_type, 'x-api-key': api_key}

如果这有助于解决这个问题,我们很乐意提供更多细节


Tags: the字符串规范apihostexecutedaterequest