我们计算的请求签名与您提供的签名不匹配。检查... - Python

发布于 2025-01-20 22:30:19 字数 1812 浏览 0 评论 0原文

我正在使用这个Python代码,在官方文档中找到了它: https://docs.aws。 amazon.com/it_it/general/latest/gr/sigv4-signed-request-examples.html

我可以在 Postman 上完成。它在那里工作!我快要疯了,你能帮帮我吗?

这是我的代码如下:

import datetime
import hashlib
import hmac
import sys
import requests  # pip install requests
# ************* REQUEST VALUES *************
method = 'GET'
service = 'sts'
host = 'amazonaws.com'
region = 'us-east-1'
endpoint = 'https://sts.amazonaws.com/'
#request_parameters = 'Action=DescribeRegions&Version=2013-10-15'
request_parameters =     'Action=AssumeRole&RoleSessionName=Test1&RoleArn=arn:aws:iam::001638200091:role/Amministratore&DurationSeconds=10000&Version=2011-06-15'

# Key derivation functions. See:
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
    kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
    kRegion = sign(kDate, regionName)
    kService = sign(kRegion, serviceName)
    kSigning = sign(kService, 'aws4_request')
return kSigning
# Read AWS access key from env. variables or configuration file. Best practice is NOT
# to embed credentials in code.
access_key = 'xxxxxxxxxxxxxxx'
secret_key = 'xxxxxxxxxxxxxxx'
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')  # Date w/o time, used in credential scope

i'm using this python code, found it on official documentation:
https://docs.aws.amazon.com/it_it/general/latest/gr/sigv4-signed-request-examples.html

I can do it on Postman. It works there! I'm going crazy, can you help me?

This is my code below:

import datetime
import hashlib
import hmac
import sys
import requests  # pip install requests
# ************* REQUEST VALUES *************
method = 'GET'
service = 'sts'
host = 'amazonaws.com'
region = 'us-east-1'
endpoint = 'https://sts.amazonaws.com/'
#request_parameters = 'Action=DescribeRegions&Version=2013-10-15'
request_parameters =     'Action=AssumeRole&RoleSessionName=Test1&RoleArn=arn:aws:iam::001638200091:role/Amministratore&DurationSeconds=10000&Version=2011-06-15'

# Key derivation functions. See:
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(key, dateStamp, regionName, serviceName):
    kDate = sign(('AWS4' + key).encode('utf-8'), dateStamp)
    kRegion = sign(kDate, regionName)
    kService = sign(kRegion, serviceName)
    kSigning = sign(kService, 'aws4_request')
return kSigning
# Read AWS access key from env. variables or configuration file. Best practice is NOT
# to embed credentials in code.
access_key = 'xxxxxxxxxxxxxxx'
secret_key = 'xxxxxxxxxxxxxxx'
if access_key is None or secret_key is None:
print('No access key is available.')
sys.exit()
# Create a date for headers and the credential string
t = datetime.datetime.utcnow()
amzdate = t.strftime('%Y%m%dT%H%M%SZ')
datestamp = t.strftime('%Y%m%d')  # Date w/o time, used in credential scope

etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文