[英]aws cognito user get id token android
I'm trying to get the current logged in user's id token for aws cognito in android.我正在尝试为 aws cognito 在 android 中获取当前登录用户的 ID 令牌。
I found this example: session.getIdToken().getJWTToken() where session is a CognitoUserSession object我找到了这个例子:session.getIdToken().getJWTToken() 其中会话是一个 CognitoUserSession 对象
I can't seem to figure out a way to get the current cognitousersession after the login call has been made.在进行登录调用后,我似乎无法找到获取当前 cognitousersession 的方法。
I'm using the default authenticator activity from the notes tutorial: https://docs.aws.amazon.com/aws-mobile/latest/developerguide/tutorial-android-aws-mobile-notes-auth.html我正在使用注释教程中的默认身份验证器活动: https ://docs.aws.amazon.com/aws-mobile/latest/developerguide/tutorial-android-aws-mobile-notes-auth.html
It says that the tokens are stored in the shared preferences, but I can't figure out how to retrieve them on future activities so that I can make calls to the api gateway using the id token.它说令牌存储在共享首选项中,但我不知道如何在未来的活动中检索它们,以便我可以使用 id 令牌调用 api 网关。
The AWS Android SDK will return the JWT token without a network call when the token is not/will not expire.当令牌没有/不会过期时,AWS Android SDK 将在没有网络调用的情况下返回 JWT 令牌。
The threshold for when a token should be refreshed can be set with the CognitoIdentityProviderClientConfig.setRefreshThreshold(long)
method.可以使用
CognitoIdentityProviderClientConfig.setRefreshThreshold(long)
方法设置何时应刷新令牌的阈值。
If you are stil curious how to retrieve the token yourself, then the code can be found in readCachedTokens()
method https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-cognitoidentityprovider/src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java#L2116如果您仍然好奇如何自己检索令牌,那么可以在
readCachedTokens()
方法中找到代码https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-cognitoidentityprovider /src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java#L2116
As nobody has answered yet, this might help you out, be aware this is JS code :由于还没有人回答,这可能会对您有所帮助,请注意这是 JS 代码:
This is my routine to receive the session from an already logged in user.这是我从已登录用户接收会话的例程。 after this, i'm able to access tokens.
在此之后,我可以访问令牌。
var user_data = {
UserPoolId: AWSConfiguration.UserPoolId,
ClientId: AWSConfiguration.ClientAppId
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(user_data);
if (userPool.getCurrentUser() != null) {
userPool.getCurrentUser().getSession(function (err, session) {
if (err) {
window.location.href = "login.html";
}
var user_params = {
IdentityPoolId: AWSConfiguration.IdPoolId,
Logins: {
'cognito-idp.eu-central-1.amazonaws.com/eu-central-1_XXX':session.idToken.jwtToken
}
};
AWS.config.credentials = new AWS.CognitoIdentityCredentials(user_params);
AWS.config.region = AWSConfiguration.region;
AWS.config.credentials.refresh((error) => {
if (error) {
console.error(error);
}
else {
user_is_authenticated();
}
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.