代码之家  ›  专栏  ›  技术社区  ›  Vingtoft

aws cognito:getcredentials不工作

  •  1
  • Vingtoft  · 技术社区  · 7 年前

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

    问题: 我拿不到 AWS.config.getCredentials 去工作。它执行时没有任何错误,但是 accessKeyId , secretAccessKey SessionToken 都是 undefined .

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Vingtoft    7 年前

    我不能让aws.config.getcredentials工作。 它的执行没有任何错误 但是,

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


    打电话时 getCredentials ,任何错误都会通过 error 反对。我想你会看到 400 在某处响应(网络选项卡或控制台或两者),但是 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
        }
    }
    

    相应的 四百 在“网络”选项卡中包含此响应:

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