简体   繁体   English

调用 AWS Cognito 安全端点时获取 401

[英]Getting 401 when making a call to AWS Cognito secured endpoint

I have a Cognito user pool.我有一个 Cognito 用户池。 The pool has an application integration for JavaScript that does not have a secret.该池具有 JavaScript 的应用程序集成,但没有密钥。 I am able to login using the following code我可以使用以下代码登录

  private static async signin(role: UserRole): Promise<string> {
    const user = getUser();

    const cognitoUser = new CognitoUser({
      Username: user.username,
      Pool: "myuserpool"
    });

    const authDetails = new AuthenticationDetails({
      Username: user.username,
      Password: user.password
    });

    return new Promise((resolve, reject): void => {
      cognitoUser.authenticateUser(authDetails, {
        onSuccess: result => {
          this.credentials[role] = result.getIdToken().getJwtToken();
          resolve(this.credentials[role]);
        },
        onFailure: err => {
          console.log(`Failed login to cognito with ${role}: `, err);
          reject(err);
        }
      });
    });
  }

When I make a call to my endpoint with the aws-api-gateway-client, I can see the token attached, but it always returns a 401 unauthorized.当我使用 aws-api-gateway-client 调用我的端点时,我可以看到附加的令牌,但它总是返回 401 未经授权。

It's super confusing because I can take this token and paste it into the ApiGateway Authorizer and receive a 200 ok.这非常令人困惑,因为我可以获取此令牌并将其粘贴到 ApiGateway Authorizer 中并收到 200 ok。 So it seems the token is valid, just not working properly.所以看起来令牌是有效的,只是不能正常工作。

EDIT: Here is the flow....编辑:这是流程....

  • I have a Cognito user pool我有一个 Cognito 用户池
  • I can login to that userpool with a username / password and get a token back我可以使用用户名/密码登录到该用户池并取回令牌
  • I then set "Authorization": "bearer {token}" on the aws-api-gateway-client request headers.然后我在 aws-api-gateway-client 请求标头上设置“授权”:“承载{令牌}”。
  • The request fails with a 401 Unauthorized请求失败并返回 401 Unauthorized
  • If I take that same token and paste it in the test section of the ApiGateway Authorizer, It tells me the token is valid.如果我使用相同的令牌并将其粘贴到 ApiGateway 授权方的测试部分,它会告诉我该令牌有效。

I don't think you need the "bearer" part for the header value.我认为您不需要 header 值的“承载”部分。 Cognito authorizers are just looking for the key/token. Cognito 授权者只是在寻找密钥/令牌。 Try a header of "Authorization": "<token_goes_here>".尝试"Authorization": "<token_goes_here>".

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Postman 或 cURL 访问时从 AWS Cognito + API 网关获取 401 Unauthorized - Getting 401 Unauthorized from AWS Cognito + API Gateway when accessing from Postman or cURL 使用 NextJS 和 Amplify 的 AWS Cognito 登录 - 在前端或后端调用端点? - AWS Cognito Signin with NextJS and Amplify - call endpoint on front or backend? AWS Cognito + API 网关 + 调用 Cognito 令牌端点 - AWS Cognito + API Gateway + Calling Cognito Token Endpoint 使用 API 密钥通过列表端点检索 Gmail 收件箱内容时出现 401 错误 - Getting 401 error when using API Key to Retrieve Gmail inbox contents via list endpoint AWS Cognito oauth2/token 端点中不允许出现 405 方法错误 - 405 method not allowed error in AWS Cognito oauth2/token endpoint Twilio - 收到错误:拨打电话时帐户 SID 不能为空 - Twilio - getting ERROR : Account SID cannot be null when making a call 如何等待 aws cognito authenticateUser 调用(这似乎是一个回调) - how to await for the aws cognito authenticateUser call (which appears to be a callback) AWS Cognito:在 Auth.signIn 中出现错误(验证 amazon-cognito-identity-js 已链接) - AWS Cognito: Getting error in Auth.signIn (Validate that amazon-cognito-identity-js has been linked) AWS Cognito JWT 身份验证适用于 ID 令牌,但不适用于访问令牌? 返回 401 - AWS Cognito JWT authentication works with the ID Token, but not the Access Token? Returns 401 AWS Cognito - 如何在使用 Google 登录时强制使用 select 帐户 - AWS Cognito - How to force select account when signing in with Google
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM