【发布时间】:2020-06-03 04:58:14
【问题描述】:
我们如何使用 boto 通过 AWS AppSync 发布 GraphQL 请求?
最终,我试图模仿一个移动应用程序访问我们在 AWS 上的 stackless/cloudformation 堆栈,但使用的是 python。不是 javascript 或放大。
主要的痛点是身份验证;我已经尝试了十几种不同的方法。这是当前的,它生成带有“UnauthorizedException”和“Permission denied”的“401”响应,考虑到我收到的其他一些消息,这实际上非常好。我现在正在使用“aws_requests_auth”库来执行签名部分。我假设它使用我本地环境中存储的/.aws/credentials 对我进行身份验证,还是这样?
对于认知身份和池将在何处以及如何进入它,我有点困惑。例如:假设我想模仿注册顺序?
无论如何,代码看起来很简单;我只是不了解身份验证。
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"
} ]
}
【问题讨论】:
-
您能否详细说明您在 AppSync API 上启用的身份验证?看起来您正在使用 API Key,它不需要 Boto 来发出请求。 (Boto 将帮助您发出 IAM 身份验证请求。)
标签: python-3.x boto3 aws-appsync