如何在AWS JS SDK v3中刷新凭证?

3

我正在尝试将我的V2应用程序迁移到V3 SDK,但在以下调用引发NotAuthorizedException并显示"Invalid login token. Token expired: 1615301743 >= 1615108625"后,我似乎无法弄清如何刷新凭据。

      credentials = await cognitoIdentity.send(
        new GetIdCommand({
          Storage: config,
          IdentityPoolId: config.get("IdentityPoolId"),
          Logins: {
            [`cognito-idp.${awsRegion}.amazonaws.com/${upid}`]: idToken,
          },
        }),
      );

在V2版本中,Credentials对象有一个叫做refresh()的方法,我可以调用它来刷新凭证。如何使用新API完成相同的操作?


你解决了吗? - BrDaHa
1个回答

0

我在以下链接中找到了以下代码示例(请查看用例4): https://www.npmjs.com/package/amazon-cognito-identity-js

  //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
        AWS.config.credentials.refresh(error => {
            if (error) {
                console.error(error);
            } else {
                // Instantiate aws sdk service objects now that the credentials have been updated.
                // example: var s3 = new AWS.S3();
                console.log('Successfully logged!');
            }
        });

当我在AWS Lambda中实现时,它对我有效。希望这就是您要寻找的。

问候,

编辑:

我刚刚测试了以下代码,在我的react-js应用程序中它可以工作:

return new Promise((resolve, reject) =>
            cognitoUser.authenticateUser(authenticationDetails, {
                // If the provided credentials are correct.
                onSuccess: function(result) {
                    var accessToken = result.getAccessToken().getJwtToken();
            
                    //POTENTIAL: Region needs to be set if not already set previously elsewhere.
                    AWS.config.region = 'us-east-1';
            
                    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                        IdentityPoolId: IdentityPoolId,           // Your identity pool id here.
                        Logins: {
                            // Change the key below according to the specific Region your User Pool is in.
                            `cognito-idp.${awsRegion}.amazonaws.com/${upid}`: result
                                .getIdToken()
                                .getJwtToken(),
                        },
                    });
                    
            
                    //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
                    AWS.config.credentials.refresh(error => {
                        if (error) {
                            console.error(error);

                        } else {                    
                            resolve(AWS.config.credentials)
                        }
                    });
                },
            
                // If the provided credentials are incorrect.
                onFailure: function(err) {
                    console.log(err);
                    reject(
                        err.message || JSON.stringify(err)
                    );
                },
            })
    );


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接