2

I am building an Angular2 application that will utilize Cognito User Pool sign-in. I am able to successfully authenticate a user; however, have been unable to get the access key, secret key, and session token for the authenticated user.

To my understanding, I should be able to call AWS.config.credentials.get( <callback> ) as shown below, but TypeScript complains that the get method is not found (though I can see the method in type declarations for the aws-sdk module).

Any ideas?

// 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 }
});
4

2 回答 2

2

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

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

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

于 2017-07-12T23:13:28.783 回答
1
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

于 2020-02-12T09:26:00.947 回答