首页 > 解决方案 > 使用 Cognito 用户池对 AppSync 查询控制台进行身份验证

问题描述

我正在尝试在 AWS AppSync 控制台中对 Queries Playground 进行身份验证。我创建了用户池并将其链接到 AppSync API,我还在 Cognito 用户池中创建了一个应用程序客户端(使用 CloudFormation 部署)。它出现Select the authorization provider to use for running queries on this page:在控制台下方。当我运行测试查询时,我得到:

{
  "errors": [
    {
      "errorType": "UnauthorizedException",
      "message": "Unable to parse JWT token."
    }
  ]
}

这是我所期望的。有一个选项Login with User Pools。问题是我无法选择任何Client ID内容,当我选择手动插入客户 ID 时,我输入的任何内容都会得到Invalid UserPoolId format。我正在尝试从用户池常规设置(格式 eu-west-2_xxxxxxxxx)中复制池 ID,但没有任何乐趣。顺便说一句,我没有使用 Amplify,也没有配置任何身份池。

在此处输入图像描述

编辑:

这是 CloudFormation GraphQLApi 定义:

  MyApi:
    Type: AWS::AppSync::GraphQLApi
    Properties:
      Name: !Sub "${AWS::StackName}-api"
      AuthenticationType: AMAZON_COGNITO_USER_POOLS
      UserPoolConfig:
        UserPoolId: !Ref UserPoolClient
        AwsRegion: !Sub ${AWS::Region}
        DefaultAction: ALLOW

标签: amazon-cognitoaws-appsyncaws-userpools

解决方案


要使用 CloudFormation 设置堆栈,我遵循了以下 2 个示例:

https://adrianhall.github.io/cloud/2018/04/17/deploy-an-aws-appsync-graphql-api-with-cloudformation/

https://gist.github.com/adrianhall/f330a10451f05a529680f32978dddb64

事实证明,他们(同一作者)在定义 ApiGraphQL 的部分中都有问题。这个:

  MyApi:
    Type: AWS::AppSync::GraphQLApi
    Properties:
      Name: !Sub "${AWS::StackName}-api"
      AuthenticationType: AMAZON_COGNITO_USER_POOLS
      UserPoolConfig:
        UserPoolId: !Ref UserPoolClient
        AwsRegion: !Sub ${AWS::Region}
        DefaultAction: ALLOW

应该:

  MyApi:
    Type: AWS::AppSync::GraphQLApi
    Properties:
      Name: !Sub "${AWS::StackName}-api"
      AuthenticationType: AMAZON_COGNITO_USER_POOLS
      UserPoolConfig:
        UserPoolId: !Ref UserPool
        AwsRegion: !Sub ${AWS::Region}
        DefaultAction: ALLOW

感谢@Myz 指点我回顾整个 CF yaml 文件


推荐阅读