首页 > 解决方案 > 如何刷新 AWS JS SDK v3 中的凭证?

问题描述

NotAuthorizedException我正在尝试将我的 V2 应用程序迁移到 V3 SDK,但在以下调用引发with后,我似乎无法弄清楚如何刷新凭据"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 中,有一个refresh()Credentials对象上调用的方法,我可以调用它并通过这样做来刷新凭据。如何用新的 API 做同样的事情?

标签: javascriptamazon-web-servicesaws-sdkaws-sdk-js

解决方案


我在以下链接中找到了以下代码示例(检查用例 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)
                    );
                },
            })
    );


推荐阅读