playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.46k stars 965 forks source link

Update idToken #3030

Open amatveev opened 3 years ago

amatveev commented 3 years ago

How update/refresh access_token/ idToken? the token's lifetime may run out! re-invoking Auth keeps returning the same token

Isaac0906 commented 2 years ago

Hi. Have you this problem yet? I found the way.

First, we need to know why this happen. You can check the reason in file 'AndroidClient.cs'.

Google prevent calling 'AndroidClient.Authenticate' method in single session.

public void Authenticate(bool silent, Action<SignInStatus> callback)
        {
            lock (AuthStateLock)
            {
                // If the user is already authenticated, just fire the callback, we don't need
                // any additional work.
                if (mAuthState == AuthState.Authenticated) ------------> This is the reason.
                {
                    Debug.Log("Already authenticated.");
                    InvokeCallbackOnGameThread(callback, SignInStatus.Success);
                    return;
                }
            }

            InitializeTokenClient();

So, I made another api in this scripts named 'TokenRefresh'

        public void TokenRefresh(Action<int> callback)
        {
            this.mTokenClient.FetchTokens(false, callback);
        }

And I want to combine this function with Authenticate logic. So, I fix Authenticate function like this.

        public void Authenticate(bool silent, Action<SignInStatus> callback)
        {
            lock (AuthStateLock)
            {
                // If the user is already authenticated, just fire the callback, we don't need
                // any additional work.
                if (mAuthState == AuthState.Authenticated)
                {
                    Debug.Log("Already authenticated.");
                    TokenRefresh((ret) =>
                    {
                        InvokeCallbackOnGameThread(callback, SignInStatus.Success);
                    });
                    return;
                }
            }

Then, you can check refresh id token. You need to know that you can have refresh token after 1 hour in same session. During first 1 hour, you have same token.