javascript - 如何从 AWS cognito 获取 awstoken
问题描述
我想在成功函数之外使用 accessToken 变量。我尝试了不同的方法来使用变量,但没有奏效。
var authenticationData = {
Username : 'username',
Password : 'password',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1_ExaMPle',
ClientId : '1example23456789'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'username',
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken();
/* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
var idToken = result.idToken.jwtToken;
},
onFailure: function(err) {
alert(err);
},
});
解决方案
为什么不添加accessToken
到authenticationData
对象?
var authenticationData = {
Username : 'username',
Password : 'password',
AccessToken : ''
};
// And then set the token in the success method:
authenticationData.AccessToken = result.getAccessToken().getJwtToken();
authenticationData.AccessToken
设置后,您可以在范围内的任何位置使用该值。
推荐阅读
- angular - 构建一个角度项目
- mysql - 执行 JPQL 查询时遇到问题
- python - MNIST - ValueError:解包的值太多(预期为 2)
- mysql - MySql 中的二级索引是什么?
- ejb - 模块重启后,WildFly 上的 Quartz Job 远程 EJB 调用失败
- python - 在 Tensorflow 的 AlexNet 实现中查找输出节点
- jmeter - 如何循环一个jmeter请求?
- android - Android MotionLayout 暂停
- javascript - 发送前更改复选框默认值
- c# - 在 c# 中使用 ---PUBLIC KEY--- 验证 JWT (ES256) 令牌