简体   繁体   English

aws cognito 用户获取 id 令牌 android

[英]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.

相关问题 如何从 AWS Cognito 上的托管 UI 获取用户池令牌 - How to get the User pool token from Hosted UI on AWS Cognito AWS Cognito 用户池:给定一个应用程序客户端 ID,如何获取其池 ID? - AWS Cognito User Pool : Given an App client id, how to get its Pool Id? AWS Cognito JWT 身份验证适用于 ID 令牌,但不适用于访问令牌? 返回 401 - AWS Cognito JWT authentication works with the ID Token, but not the Access Token? Returns 401 如何在 iOS 中使用 AWSMobileClient 获取 AWS Cognito 用户属性? - How to get AWS Cognito user attributes using AWSMobileClient in iOS? 在预身份验证用户的情况下如何使用 amazon cognito 获取刷新令牌 - How to get refresh token using amazon cognito in case of Preauthenticated User AWS Lambda - 无法在 lambda 函数中获取 Cognito 用户数据 - AWS Lambda - Can't get Cognito user data in lambda function 我无法获取用户的 AWS Cognito 凭证 (swiftUI) - I cannot get the AWS Cognito credentials of a user (swiftUI) 手动设置 AWS Cognito 访问令牌超时 - Set AWS Cognito access token timeout manually 如何将 AWS Cognito 用户与 DynamoDB 同步 - How to sync AWS Cognito user with DynamoDB AWS cognito 在谷歌身份验证后检索刷新令牌 - AWS cognito to retrieving refresh token after google authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM