【发布时间】:2021-10-29 21:12:48
【问题描述】:
我有一个由 amplify 创建的 lambda 函数,用于从 appsync 获取捐助者列表,但每次我尝试请求时它都会得到 UnauthorizedException。这是我的 lambda 函数:
const axios = require('axios');
const gql = require('graphql-tag');
const graphql = require('graphql');
const { print } = graphql;
const listDonors = gql`
query listDonors {
listDonors {
items {
id
firstName
lastName
}
}
}
`
exports.handler = async (event) => {
console.log("--------------------------------->");
try {
const graphqlData = await axios({
url: process.env.API_DOCBACKEND_GRAPHQLAPIENDPOINTOUTPUT,
method: 'post',
headers: {
'x-api-key': process.env.API_DOCBACKEND_GRAPHQLAPIIDOUTPUT
},
data: {
query: print(listDonors),
}
});
const body = {
graphqlData: graphqlData.data.data.listTodos
}
return {
statusCode: 200,
body: JSON.stringify(body),
headers: {
"Access-Control-Allow-Origin": "*",
}
}
} catch (err) {
console.log('error posting to appsync: ', err);
}
}
这是我的 IAM 角色:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"appsync:Create*",
"appsync:StartSchemaCreation",
"appsync:GraphQL",
"appsync:Get*",
"appsync:List*",
"appsync:Update*",
"appsync:Delete*"
],
"Resource": [
"arn:aws:appsync:us-east-1:862148551361:apis/2i62fn5z4vhtxbik3jcm33tc6e/types/Query/*",
"arn:aws:appsync:us-east-1:862148551361:apis/2i62fn5z4vhtxbik3jcm33tc6e/types/Mutation/*",
"arn:aws:appsync:us-east-1:862148551361:apis/2i62fn5z4vhtxbik3jcm33tc6e/types/Subscription/*"
],
"Effect": "Allow"
}
]
}
我遵循 aws amplify 文档,但它对我没有任何帮助。
【问题讨论】:
-
发布您的错误信息
-
这是我的错误信息:错误:请求失败,状态码为 401
标签: aws-lambda aws-amplify aws-appsync