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

问题描述 投票:0回答:2

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

这是初始化 google api 客户端的代码:

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();

这是获取谷歌登录结果的代码:

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();
        }
    }
}

与谷歌示例代码几乎相同。

问题是昨天我通过这种方式拿到了token,而且token是有效的。今天还是这样取token但是无效。 今天和昨天的代币是一样的。我认为令牌昨天过期了,今天我会得到一个新令牌,但我仍然得到相同的令牌。

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

所以情况很奇怪。不同的设备有不同的令牌。即使一天后,使用同一设备始终会获得相同的令牌。

有人对这个问题有想法吗?非常感谢。

android authentication token google-signin
2个回答
0
投票

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


0
投票

默认情况下,访问令牌的有效期为 1 小时,这似乎足以授权登录用户数据。在您与后端服务器验证令牌后,我们必须为用户建立会话。

© www.soinside.com 2019 - 2024. All rights reserved.