如何使用Android SDK刷新AWS Cognito中的令牌?

时间:2018-06-22 05:06:21

标签: android amazon-web-services amazon-cognito

当联邦身份终止后,AWS生成的令牌到期后,我一直在寻找刷新令牌的适当方法。我的应用程序使用cognito登录并注册用户,然后使用Access Token,然后使用RetroFit进入api。

改造电话

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        //end

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.readTimeout(45, TimeUnit.SECONDS);
        httpClient.connectTimeout(120, TimeUnit.SECONDS);
        httpClient.interceptors().add(new AuthInterceptor());
        httpClient.addInterceptor(interceptor); //debugging

        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();

                // Customize the request
                Request request = original.newBuilder()
                        .header("Content-type", "application/json")
                        .header("Authorization", "auth-token")
                        .header("Accept", "application/json")
                        .header("deviceType", "android") //experimental
                        .header("Version", "v1")
                        .method(original.method(), original.body())
                        .build();

                Response response = chain.proceed(request);
                return response;
            }
        });

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .client(httpClient.build())
                .addConverterFactory(ToStringConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(RetroInterface.class);

AuthInterceptor

...
  if (jsonObject.getInt("status") == 0 && jsonObject.getJSONObject("data").getInt
                    ("code")
                    == 99) {
                IS_TOKEN_EXIPRED = true;

//                HashMap<String, String> logins = new HashMap<String, String>();
                logins.put("cognito-idp.us-east-1.amazonaws.com/" + BuildConfig.USERPOOLID,
                        Cognito.getInstance().getCurrSession().getIdToken().getJWTToken());

                CognitoLogin.credentialsProvider.setLogins(logins);

                String newtoken = Cognito.getInstance().getCurrSession().getAccessToken().getJWTToken();

                Log.e("refy", newtoken);

                BaseApplication.getApp().doSaveSharedString(AppConstants.USER_ACCESS_TOKEN, newtoken);
                BaseApplication.getApp().doWriteLog("AuthInterceptor Here: ");
            }

        } catch (JSONException e) {
            BaseApplication.getApp().redirectToLoginActivity();
            e.printStackTrace();
        }
...

我遇到了这个问题。,令牌在1小时后过期,然后我什么也不能做。在Android中没有可用于刷新令牌的信息。我所看到的是,只要刷新令牌有效,Android AWS SDK就会自己刷新令牌。

1 个答案:

答案 0 :(得分:2)

因此,在网上搜索三天后,我得到了答案。 您所需要做的就是调用getSession(..)来获取刷新的令牌。 这是如何完成的:

 Cognito.getPool().getUser().getSession(new CognitoLogin(BaseApplication.getApp().getApplicationContext(), Cognito.getCurrUser(), Cognito.getPasswordForFirstTimeLogin()));
                String newtoken =  Cognito.getInstance().getCurrSession().getAccessToken().getJWTToken();

参考 Cognito User Pool: How to refresh Access Token Android