简体   繁体   English

适用于 Android 的 Google 登录 ID 令牌无效

[英]Google Sign-In for Android Invalid ID Token

I am following the instructions on the Google Sign-In for Android guide ( https://developers.google.com/identity/sign-in/android/backend-auth ), and I am trying to validate my ID token.我正在按照 Android 版 Google 登录指南 ( https://developers.google.com/identity/sign-in/android/backend-auth ) 上的说明操作,我正在尝试验证我的 ID 令牌。 When I go to https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 in my browser (where XYZ123 is my id token I retrieved using String idToken = acct.getIdToken(); , I get this response:当我在浏览器中访问https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123 (其中 XYZ123 是我使用String idToken = acct.getIdToken();检索到的 ID 令牌时,我得到了这个响应:

{
 "error_description": "Invalid Value"
}

My id token starts with eyJ and is 1038 characters long.我的 id 令牌以 eyJ 开头,长度为 1038 个字符。

I've also tried the solution here Android : Google Sign-in Invalid token to no avail.我也试过这里的解决方案Android : Google Sign-in Invalid token无济于事。

Any help would be appreciated.任何帮助,将不胜感激。

It turns out my token just expired.原来我的令牌刚刚过期。 I grabbed a new token and it works!我抓了一个新令牌,它起作用了!

Had the same problem.有同样的问题。 For me clearing app data completely solved the issue.对我来说,清除应用程序数据完全解决了这个问题。 Seemed like an old expired token got stuck.好像一个旧的过期令牌卡住了。

From my own answer in another question:来自我在另一个问题中的回答:

In my case, I was testing this in Unity and I copied the idToken value that I printed in logcat.就我而言,我在 Unity 中对此进行了测试,并复制了我在 logcat 中打印的idToken值。 Turns out, there is some character or size limit (1024 bytes?) for a line in either adb logcat or Unity's Debug.Log() method.事实证明,在 adb logcat 或 Unity 的Debug.Log()方法中,对于一行有一些字符或大小限制(1024 字节?)。 So the printed token value was getting truncated.所以打印的令牌值被截断了。 What I did then for testing was that I copied the token value to clipboard during runtime and then checked again with the tokeninfo endpoint https://oauth2.googleapis.com/tokeninfo?id_token= and it was accepted.我当时所做的测试是在运行时将令牌值复制到剪贴板,然后使用 tokeninfo 端点https://oauth2.googleapis.com/tokeninfo?id_token=再次检查并被接受。

If the idToken that you are passing to the function is from the log of your mobile app, it is likely that you are not getting the entire idToken printed in the log due to the limitations of generic log.如果您传递给函数的 idToken 来自您的移动应用程序的日志,则由于通用日志的限制,您可能没有在日志中打印整个 idToken。

I used the below code snippet to print out the idToken and used that in the API which gave me a success response.我使用下面的代码片段打印出 idToken 并在 API 中使用它,这给了我一个成功的响应。

print('ID TOKEN');
String token = googleAuth.idToken;
while (token.length > 0) {
    int initLength = (token.length >= 500 ? 500 : token.length);
    print(token.substring(0, initLength));
    int endLength = token.length;
    token = token.substring(initLength, endLength);
}

Note: Although this snippet is flutter-specific, you can reuse the logic in any framework.注意:尽管此代码段是特定于 flutter 的,但您可以在任何框架中重用该逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM