【问题标题】:AWS Amplify AppSync IAM 401AWS Amplify AppSync IAM 401
【发布时间】:2018-10-27 21:19:35
【问题描述】:

我收到GraphQLError: Request failed with status code 401

我遵循了来自:

的自动配置说明

https://aws.github.io/aws-amplify/media/api_guide#automated-configuration-with-cli

我尝试查找,但 IAM 缺乏资源。看起来一切都应该自动设置,并在我输入 IAM 访问密钥和机密后使用 Amplify CLI 完成。

是否需要进一步设置?这是我的代码:

import Amplify, { API, graphqlOperation, Hub } from "aws-amplify";
import aws_config from "../../aws-exports";

Amplify.configure(aws_config);

const ListKeywords = `query ListKeywords {
  listKeyword {
    keyword {
      id
      name
    }
  }
}`;

const loop = async () => {
  const allKeywords = await API.graphql(graphqlOperation(ListKeywords));
}

还可能是因为我的 GraphQL 解析器还没有为 ListKeywords 设置吗?

【问题讨论】:

    标签: amazon-web-services amazon-iam aws-appsync aws-amplify


    【解决方案1】:

    如果您在 AppSync API 上使用 IAM 作为授权类型,那么问题是在调用 Amplify.configure() 时与 Auth 类别一起使用的 Cognito 角色未被授予 GraphQL 操作的权限。它需要这样的附件:

    {
       "Version": "2012-10-17",
       "Statement": [
          {
             "Effect": "Allow",
             "Action": [
                "appsync:GraphQL"
             ],
             "Resource": [
                "arn:aws:appsync:us-west-2:123456789012:apis/YourGraphQLApiId/*"
             ]
          }
       ]
    }
    

    更多详情:https://docs.aws.amazon.com/appsync/latest/devguide/security.html

    【讨论】:

    【解决方案2】:

    不确定这是否有帮助,但我已经为此苦苦挣扎了一段时间,发现如果我添加 API 并使用 IAM 作为身份验证方法,我也需要将“身份验证”添加到架构中。

    见下文:

    type TimeLapseCamera @model 
    @auth(rules: [
      { allow: private, provider: iam }
    ])
    {
    ...
    }
    

    我刚刚测试了这个,我的网页成功添加了一条记录。

    注意其他评论;我根本没有 AWS - 它是一个带有 Amplify 的简单 VUE 应用程序。

    【讨论】:

      【解决方案3】:

      我刚刚更改了~/.aws/credentials,现在它可以工作了。

      看起来即使您通过 Amplify 的命令行工具或 ~/.awsmobile/aws-config.js 进行项目特定配置,它仍然依赖于 ~/.aws

      【讨论】:

      • 有没有办法避免 ~/.aws 覆盖您的 Amplify 配置?这在配置了 ~/.aws 目录的人的系统上破坏了我的应用程序
      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 2021-08-13
      • 2018-08-19
      • 2019-12-27
      • 2021-08-23
      • 1970-01-01
      • 2019-10-19
      • 2020-12-28
      相关资源
      最近更新 更多