Javascript 如何使用Cognito标识池生成临时凭证以访问aws服务?

Javascript 如何使用Cognito标识池生成临时凭证以访问aws服务?,javascript,amazon-web-services,amazon-cognito,aws-userpools,Javascript,Amazon Web Services,Amazon Cognito,Aws Userpools,我有一个cognito用户池和身份池。我已在用户池中创建了一个用户。我使用lambda获得了该用户的令牌,即访问、刷新、id令牌。现在,我想生成临时凭证,即访问密钥,并为该用户保密访问密钥,以访问aws服务。我怎么能这样做? 这是我用来生成令牌的一段代码 var authenticationDetails = new cognito.AuthenticationDetails(authenticationData); var userData = { Username

我有一个cognito用户池和身份池。我已在用户池中创建了一个用户。我使用lambda获得了该用户的令牌,即访问、刷新、id令牌。现在,我想生成临时凭证,即访问密钥,并为该用户保密访问密钥,以访问aws服务。我怎么能这样做? 这是我用来生成令牌的一段代码

var authenticationDetails = new cognito.AuthenticationDetails(authenticationData);

    var userData = {
        Username : '*****',
        Pool : userPool
    };

var cognitoUser = new cognito.CognitoUser(userData);
    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
console.log(result);

我应该对此进行哪些更改以获取凭据?谢谢……

在下面的代码中,我正在检索令牌以及基于联邦身份的临时cred

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);
      }
    });
  });
} 
根据您的需要更改必要的参数


希望它可以帮助您

HI@Vivek,如何获取
访问、刷新、id令牌
?要获取令牌,您必须创建用户,然后对用户进行身份验证。是的,我明白了。谢谢你的回复谢谢!我缺少的部分是AWS.config.credentials.get()。我看到我的证件已经过期了,我很困惑。您在文档中的什么地方找到的?