AWS Cognito:Missing' get' AWS.config.credentials的方法

时间:2017-03-06 00:26:28

标签: angular amazon-web-services amazon-cognito typescript2.0 aws-cognito

我正在构建一个将使用Cognito User Pool登录的Angular2应用程序。我能够成功验证用户;但是,无法获取经过身份验证的用户的访问密钥,密钥和会话令牌。

根据我的理解,我应该能够调用AWS.config.credentials.get( <callback> ),如下所示,但是TypeScript抱怨找不到get方法(虽然我可以在aws的类型声明中看到方法 - sdk模块)。

有什么想法吗?

// As part of authenticateUser - onSuccess callback...
var logins = {};
logins[`cognito-idp.${CognitoHelper.REGION}.amazonaws.com/${CognitoHelper.USER_POOL_ID}`] = session.getIdToken().getJwtToken();

// Add the user's token to the credential map
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: CognitoHelper.IDENTITY_POOL_ID,
  Logins: logins
});

// Get access keys
AWS.config.credentials.get( (error) => {  <-- .get method not found!
  if (error) { // do something }
});

2 个答案:

答案 0 :(得分:2)

对于那些感兴趣的人,我最终通过强制转换为AWS.Credentials来解决:

(AWS.config.credentials as AWS.Credentials).get( (err) => { } )

我不是TypeScript专家,但这解决了编译器问题。

答案 1 :(得分:0)

AWS.config.getCredentials(function (err) {
     if (err) console.log(err.stack); // credentials not loaded
     else console.log("Access Key:", AWS.config.credentials.accessKeyId);
})

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#getCredentials-property