AWS cognito:getCredentials 不工作

AWS cognito: getCredentials not working

我正在学习使用 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

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

I cant get the AWS.config.getCredentials to work. It executed without any errors but,

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


调用 getCredentials 时,任何错误都会通过 error 对象 "silently" 报告。我认为您会在某处(网络选项卡或控制台或两者)看到 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"}