最终,我想要模仿移动应用程序在AWS的无堆栈/CloudFormation堆栈上访问,但是使用Python而不是JavaScript或Amplify。
主要痛点是身份验证;我已经尝试了十几种不同的方法。这是当前的一种方式,它生成带有“UnauthorizedException”和“Permission denied”的“401”响应,考虑到我收到的其他消息,这实际上相当不错了。现在我正在使用'aws_requests_auth'库来完成签名部分。我假设它使用我的本地环境中存储的
/.aws/credentials
进行身份验证,但它真的这样吗?我有点困惑cognito身份和池将如何涉及其中。例如:假设我想模拟注册流程?
总之,代码看起来非常简单;我只是不理解身份验证。
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
APPSYNC_API_KEY = 'inAppsyncSettings'
APPSYNC_API_ENDPOINT_URL = 'https://aaaaaaaaaaaavzbke.appsync-api.ap-southeast-2.amazonaws.com/graphql'
headers = {
'Content-Type': "application/graphql",
'x-api-key': APPSYNC_API_KEY,
'cache-control': "no-cache",
}
query = """{
GetUserSettingsByEmail(email: "john@washere"){
items {name, identity_id, invite_code}
}
}"""
def test_stuff():
# Use the library to generate auth headers.
auth = BotoAWSRequestsAuth(
aws_host='aaaaaaaaaaaavzbke.appsync-api.ap-southeast-2.amazonaws.com',
aws_region='ap-southeast-2',
aws_service='appsync')
# Create an http graphql request.
response = requests.post(
APPSYNC_API_ENDPOINT_URL,
json={'query': query},
auth=auth,
headers=headers)
print(response)
# this didn't work:
# response = requests.post(APPSYNC_API_ENDPOINT_URL, data=json.dumps({'query': query}), auth=auth, headers=headers)
产量
{
"errors" : [ {
"errorType" : "UnauthorizedException",
"message" : "Permission denied"
} ]
}