在下面的代码中,我正在检索令牌以及基于联合身份的临时信用。
var data = {
UserPoolId: YOURUSER_POOL_ID,
ClientId: YOURAPP_CLIENT_ID,
};
var userPool = new cognito.CognitoUserPool(data);
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if (err) {
console.log(err);
return;
}
console.log('session validity: ' + session.isValid());
console.log('session Identity token: ' + session.getIdToken().getJwtToken());
AWS.config.region = YOURREGION;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : YOURIDENTITY_POOL_ID,
Logins : {
// Change the key below according to the specific region your user pool is in.
'cognito-idp.YOURREGIONNAME.amazonaws.com/YOURUSERPOOLID': session.getIdToken().getJwtToken()
}
});
AWS.config.credentials.get(function(err,data) {
if (!err) {
var id = AWS.config.credentials.identityId;
var key = AWS.config.credentials.accessKeyId;
var secretkey = AWS.config.credentials.secretAccessKey;
var sessionToken = AWS.config.credentials.sessionToken;
console.log('Cognito Identity ID '+ id);
console.log('Cognito Key '+ key);
console.log('Cognito Secret Key '+ secretkey);
console.log('Cognito SessionToken '+ sessionToken);
}
});
});
}
根据您的更改必要的参数。
希望它可以帮助你