gpt4 book ai didi

android - Google 始终返回过期的 ID token (JWT)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:08 26 4
gpt4 key购买 nike

根据 https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInApi.html#constant-summary

If you use the ID token expiry time to determine your session lifetime, you should retrieve a refreshed ID token, by calling silentSignIn prior to each API call to your application server.

我正在尝试通过调用 silentSignIn 来获取新 token 。但是,我总是得到相同的过期 ID token 。有人可以帮我指出显示如何强制刷新以获取新 token 的正确文档吗?我的代码没什么特别的。它与我们在 google 的示例中所拥有的几乎相同。

private void buildGoogleApiClient()
{
// Configure sign-in to request the user's ID, email address, and basic profile. ID and
// basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//.requestServerAuthCode(serverClientId)
.requestIdToken(SERVER_ID)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestEmail()
.build();

// Build a GoogleApiClient with access to GoogleSignIn.API and the options above.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}

我如上所述构建我的 API,当用户点击使用 google 登录按钮时,我执行以下操作

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.
Timber.d("Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
}
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.
Timber.d("Checking if user has already given access to this app on an other device.");
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>()
{
@Override
public void onResult(GoogleSignInResult googleSignInResult)
{
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}

handleSignInResult 方法将获取 id token 作为

GoogleSignInAccount acct = result.getSignInAccount();
String idToken = acct.getIdToken();
Timber.d(acct.getIdToken());

编辑 1

我还有一个相关的问题。

在我撤销了 https://security.google.com/settings/u/1/security/permissions 的权限后我仍然能够检索 ID token (JWT)。假设此 token 已缓存并且 Android 播放服务从本地副本提供它。 token 过期(60 分钟)后,播放服务应联系 Google 的服务以获取最新状态。但是,我没有看到这种情况发生。事实上,我注意到的另一件奇怪的事情是,当我在大约一天后调用 silentSignIn() 时,我在未经用户同意的情况下获得了一个新 token 。有人可以测试这个用例并让我知道输出吗?

我如何确保在用户撤消权限后再次请求他们授予权限。

最佳答案

您将需要使用 startActivityForResult(),然后在 onActivityResult() 中处理登录结果,如此处所述 Integrating Google Sign-In into Your Android App .然后,要验证 token 的真实性,请参阅 Google 的 Android 版 Google 登录指南,了解如何 Authenticate with a backend server .

一种简单的方法是将 token 传递给 Google tokeninfo 端点https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 其中 XYZ123 是您的 token 。

关于android - Google 始终返回过期的 ID token (JWT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37363213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com