gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:49:43 26 4
gpt4 key购买 nike

我正在尝试将我的 V2 应用程序迁移到 V3 SDK,但在以下调用抛出 NotAuthorizedException with "Invalid login 后,我似乎无法弄清楚如何刷新凭据 token 。 token 已过期: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 做同样的事情?

最佳答案

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

关于javascript - 如何在 AWS JS SDK v3 中刷新凭证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66549579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com