从 aws cognito 检索访问令牌、秘密访问密钥和会话令牌

Retrieve the access token, secret access key and session token from aws cognito

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
      IdentityPoolId: 'us-east-1:2ce7b2c2-898f-4a26-9066-d4feff8ebfe4'
  });

  // Make the call to obtain credentials
  AWS.config.credentials.get(function(){

      // Credentials will be available when this function is called.
      var accessKeyId = AWS.config.credentials.accessKeyId;
      var secretAccessKey = AWS.config.credentials.secretAccessKey;
      var sessionToken = AWS.config.credentials.sessionToken;
      //var identityId = AWS.config.credentials.identityId;

      return res.send({
        accessKeyId: accessKeyId
      });

  });

所有变量均为空值。为什么?我究竟做错了什么?还有其他访问方式吗?

我还应该发送密钥和令牌来检索会话密钥

更新:

当我尝试这个方法时,我收到一条错误消息: 错误:NotAuthorizedException:此身份池不支持未经身份验证的访问。

AWS.config.credentials.get(function(err) {
    if (err) {
        console.log("Error: "+err);
        return;
    }
    console.log("Cognito Identity Id: " + AWS.config.credentials.identityId);

    // Other service clients will automatically use the Cognito Credentials provider
    // configured in the JavaScript SDK.
    var cognitoSyncClient = new AWS.CognitoSync();
    cognitoSyncClient.listDatasets({
        IdentityId: AWS.config.credentials.identityId,
        IdentityPoolId: ""
    }, function(err, data) {
        if ( !err ) {
            console.log(JSON.stringify(data));
        }
        return res.send({
          data: data
        });
    });
  });

您看到的异常意味着您没有设置身份池以允许未经身份验证的身份。

调用获取凭据时,您没有在登录映射中传递任何登录信息,这意味着您的用户未通过身份验证(您的身份池不允许这样做)。

这里有一些文档描述了如何使用外部身份提供商进行身份验证: http://docs.aws.amazon.com/cognito/latest/developerguide/external-identity-providers.html