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

对我做错了什么建议吗?

最佳答案

我无法使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"}

关于node.js - AWS Cognito:getCredentials不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50655839/

10-16 23:58