谷歌登录Android,令牌到期,使用后端服务器进行身份验证

时间:2016-04-05 15:46:03

标签: android authentication token google-signin

我将google登录集成到我的项目中,并使用后端服务器进行身份验证。 我按照谷歌提供的示例代码。

这是init google api client的代码:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(Constants.GoogleSignIn.SERVER_CLIENT_ID)
                .requestScopes(new Scope(Scopes.EMAIL))
                .build();

        // Build GoogleAPIClient with the Google Sign-In API and the above options.
        client = new GoogleApiClient.Builder(context.getApplicationContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

以下是获取Google登录结果的代码:

private void handleSignInResult(@NonNull GoogleSignInResult result)
{
    BHLog.d(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess())
    {
        GoogleSignInAccount acct = result.getSignInAccount();
        if (acct != null)
        {
            String idToken = acct.getIdToken();
            Managers.getLoginManager().loginWithGoogle(idToken);
        }
    }
    else
    {
        if (listener != null)
        {
            listener.onGoogleLoginFailed();
        }
    }
}

与Google示例代码几乎相同。

问题是昨天我以这种方式获得了令牌并且令牌有效。今天我仍以这种方式获取令牌,但它无效。 今天和昨天的代币都是一样的。我认为令牌已于昨天过期,今天我会得到一个新令牌,但我仍然有同样的令牌。

然后我使用了另一台设备并获取了令牌。我有一个新的,它是有效的。

所以条件很奇怪。不同的设备有不同的令牌。即使在一天之后,使用相同的设备也会获得相同的令牌。

任何人都有关于这个问题的想法?非常感谢你。

1 个答案:

答案 0 :(得分:0)

您的设备上应该有正确的时间和日期。如果还没有,你将通过无效的exp时间。