【问题标题】:AWS cognito: getCredentials not workingAWS cognito:getCredentials 不起作用
【发布时间】:2018-11-12 07:53:07
【问题描述】:

我正在学习使用 AWS Cognito。我已经设置了一个用户池和一个身份池。

代码(简体):

cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: (result) => {
        let cognitoGetUser = userPool.getCurrentUser();
        if (cognitoGetUser != null) {
          cognitoGetUser.getSession((err, result) => {
            if (result) {
              console.log ("Authenticated to Cognito User and Identity Pools!");
              let token = result.getIdToken().getJwtToken();
              let cognitoParams = {
                IdentityPoolId: this.identityPool,
                Logins: {}
              };
              cognitoParams.Logins["cognito-idp.eu-west-1.amazonaws.com/"+this.poolData.UserPoolId] = token;
              AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams);

              AWS.config.getCredentials(() => {
                  console.log(AWS.config.credentials.accessKeyId)
                  console.log(AWS.config.credentials.secretAccessKey)
                  console.log(AWS.config.credentials.sessionToken)  
              }
            }
          }
        }
      },
      onFailure: function(err) {
        console.log('error');
        console.log(err)
      }
    }
  }

大部分代码按预期工作:authenticateUser 触发 onSuccess,我可以看到 jwt 令牌等

问题:我无法让AWS.config.getCredentials 工作。它执行时没有任何错误,但accessKeyIdsecretAccessKeySessionToken 都是undefined

对我做错了什么有什么建议吗?

【问题讨论】:

    标签: node.js amazon-web-services amazon-cognito federated-identity


    【解决方案1】:

    我无法让 AWS.config.getCredentials 工作。 它执行没有任何错误但是,

    这可能是一个错误的假设。您的缩写代码缺少几个右括号,但对我来说运行良好,没有任何有意义的调整。


    调用getCredentials 时,任何错误都会通过error 对象“静默”报告。我认为您会在某处(网络选项卡或控制台或两者)看到400 响应,但getCredentials() 本身并没有真正以可见的方式报告错误。

    要查看发生了什么问题,您应该在传递给getCredentials() 的回调中添加一个参数:

    AWS.config.getCredentials((err) => {
        if (err) {
            console.log(err);
        } else {
            console.log(AWS.config.credentials.accessKeyId)
            console.log(AWS.config.credentials.secretAccessKey)
            console.log(AWS.config.credentials.sessionToken)
        }
    });
    

    作为参考,一个常见的错误对象如下所示。请注意,有用的消息位于originalError.message

    {
        "message": "Could not load credentials from CognitoIdentityCredentials",
        "code": "CredentialsError",
        "time": "2018-06-03T15:19:02.078Z",
        "requestId": "71b03b4a-6741-11e8-98af-b70a114474f8",
        "statusCode": 400,
        "retryable": false,
        "retryDelay": 94.28032122526344,
        "originalError": {
            "message": "Invalid login token. Issuer doesn't match providerName",
            "code": "NotAuthorizedException",
            "time": "2018-06-03T15:19:02.078Z",
            "requestId": "71b03b4a-6741-11e8-98af-b70a114474f8",
            "statusCode": 400,
            "retryable": false,
            "retryDelay": 94.28032122526344
        }
    }
    

    网络选项卡中对应的400 包含此响应:

    {"__type":"NotAuthorizedException","message":"Invalid login token. Issuer doesn't match providerName"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 2016-08-02
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多