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

时间:2016-02-26 21:17:42

标签: javascript node.js authentication amazon-web-services amazon-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
        });
    });
  });

1 个答案:

答案 0 :(得分:0)

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

当您调用get凭据时,您没有在登录映射中传递任何登录,这意味着您的用户未经过身份验证(身份池不允许)。

以下是一些描述如何使用外部身份提供程序进行身份验证的文档: http://docs.aws.amazon.com/cognito/latest/developerguide/external-identity-providers.html