簡體   English   中英

aws cognito 用戶獲取 id 令牌 android

[英]aws cognito user get id token android

我正在嘗試為 aws cognito 在 android 中獲取當前登錄用戶的 ID 令牌。

我找到了這個例子:session.getIdToken().getJWTToken() 其中會話是一個 CognitoUserSession 對象

在進行登錄調用后,我似乎無法找到獲取當前 cognitousersession 的方法。

我正在使用注釋教程中的默認身份驗證器活動: https ://docs.aws.amazon.com/aws-mobile/latest/developerguide/tutorial-android-aws-mobile-notes-auth.html

它說令牌存儲在共享首選項中,但我不知道如何在未來的活動中檢索它們,以便我可以使用 id 令牌調用 api 網關。

當令牌沒有/不會過期時,AWS Android SDK 將在沒有網絡調用的情況下返回 JWT 令牌。

可以使用CognitoIdentityProviderClientConfig.setRefreshThreshold(long)方法設置何時應刷新令牌的閾值。

如果您仍然好奇如何自己檢索令牌,那么可以在readCachedTokens()方法中找到代碼https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-cognitoidentityprovider /src/main/java/com/amazonaws/mobileconnectors/cognitoidentityprovider/CognitoUser.java#L2116

由於還沒有人回答,這可能會對您有所幫助,請注意這是 JS 代碼

這是我從已登錄用戶接收會話的例程。 在此之后,我可以訪問令牌。

  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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM