使用 Python 中的 IAM 角色访问 AWS API Gateway

2024-05-10

我有一个 AWS API 网关,我想使用它来保护其安全IAM 角色 http://docs.aws.amazon.com/apigateway/latest/developerguide/permissions.html .

我正在寻找一个包来帮助我使用 Python 访问它。我试图避免实施整个版本 4 签名流程 http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html。我确信一定有一些我可以使用的图书馆。

我调查了AWS 请求身份验证 https://github.com/DavidMuller/aws-requests-auth但它需要“aws_service”来生成签名。我还查看了 boto3,但我找不到任何方法将身份验证标头添加到一般请求中。


您可以使用AWS 请求身份验证 https://github.com/DavidMuller/aws-requests-auth为您对 API Gateway 的请求生成签名执行API作为服务名称。

import requests
from aws_requests_auth.aws_auth import AWSRequestsAuth

auth = AWSRequestsAuth(aws_access_key='YOURKEY',
                       aws_secret_access_key='YOURSECRET',
                       aws_host='restapiid.execute-api.us-east-1.amazonaws.com',
                       aws_region='us-east-1',
                       aws_service='execute-api')

headers = {'params': 'ABC'}
response = requests.get('https://restapiid.execute-api.us-east-1.amazonaws.com/stage/resource_path',
                        auth=auth, headers=headers)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python 中的 IAM 角色访问 AWS API Gateway 的相关文章

随机推荐