在Android上集成Google登录后,如何在ID令牌过期后刷新? [英] How to refresh ID token after it expires when integrating Google sign-in on Android?

查看:252
本文介绍了在Android上集成Google登录后,如何在ID令牌过期后刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照 https://developers.google.com/上的指南进行操作,我可以获得用户登录后,该登录用户的Google ID令牌.但是我注意到该令牌将在1小时后过期.我找不到任何官方参考资料可以告诉我如何处理过期的Google ID令牌,因此只能要求用户再次单击Google登录按钮.

I've followed the guides on https://developers.google.com/ and I can get the Google ID token of the signed in user after the user signed in. But I noticed that the token will expire in 1 hour. I cannot find any official reference that tells me how to deal with expired Google ID token, so I can only ask the user to click the Google sign-in button again.

在旧的ID令牌过期后,如何刷新有效的Google ID令牌,而又不打扰用户一次又一次手动登录?

How can I refresh a valid Google ID token after the old one expires, without bothering user to manually sign in again and again?

推荐答案

是的,Google ID令牌的有效期为一小时,并且该令牌将过期,您只需使用

Yes, Google ID tokens are issued for one hour validity and will expire, you can simply use silentSignIn in your app to get a new one without any user interaction. If your existing token hasn't expired yet, you will get the (cached) version back (OptionalPendingResult returned will have isDone() == true); if it expired already, you will get a refreshed one (but it will take a little longer and thus OptionalPendingResult isDone() will be false).

这里是

Here is sample code (UI thread, see note below about a worker thread):

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.server_client_id))

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
...

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone()) {
        // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
        // and the GoogleSignInResult will be available instantly.
        Log.d(TAG, "Got cached sign-in");
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);  // result.getSignInAccount().getIdToken(), etc.
    } else {
        // If the user has not previously signed in on this device or the sign-in has expired,
        // this asynchronous branch will attempt to sign in the user silently.  Cross-device
        // single sign-on will occur in this branch.
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                handleSignInResult(googleSignInResult);  // result.getSignInAccount().getIdToken(), etc.
            }
        });
    }

请记住在UI线程还是辅助线程上调用silentSignIn.如果在辅助线程上调用它,请使用blockingConnect() + await()来查看此文章,这大大简化了代码: 静音登录以使用GoogleApiClient检索令牌

Keep in mind whether you call silentSignIn on a UI thread or worker thread. If you call it on worker thread, take a look at this post with blockingConnect() + await() which simplifies the code a lot: Silent sign in to retrieve token with GoogleApiClient

这篇关于在Android上集成Google登录后,如何在ID令牌过期后刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆