【发布时间】:2019-02-13 01:26:25
【问题描述】:
我正在尝试使用普通 Apollo 客户端连接到我的 AWS AppSync API,但我不确定如何正确构建身份验证标头。
到目前为止,我一直遵循这里的标头身份验证文档:https://www.apollographql.com/docs/react/recipes/authentication.html
并且有这个代码,我调整它以包含对 Amplify 身份验证服务的令牌调用,但它返回 401 错误:
const httpLink = createHttpLink({
uri: '[API end point address]/graphql'
});
const authLink = setContext((_, { headers }) => {
const token = async () => (await Auth.currentSession()).getAccessToken().getJwtToken();
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ""
}
}
})
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
})
我能找到的唯一相关文档没有提供任何技术说明:
使用 Amazon Cognito 用户池时,您可以创建用户组 属于。此信息编码在您的 JWT 令牌中 应用程序在授权标头中发送到 AWS AppSync 时 发送 GraphQL 操作。
从这里:https://docs.aws.amazon.com/appsync/latest/devguide/security.html
我知道令牌很好,因为如果我使用 AppSync JavaScript API,它就可以工作。有什么地方我可以去了解如何实现这一目标,或者有人知道如何实现吗?
编辑:
到目前为止,我已尝试更改此行:
authorization: token ? `Bearer ${token}` : ""
以下尝试:
token
jwtToken: token
authorization: token
Authorization: token
这些都没有奏效。
【问题讨论】:
标签: javascript amazon-web-services graphql apollo aws-appsync