我正在尝试使用插件在我的 flutter 应用程序上实现社交身份验证 google_sign_in plugin .它在客户端正常工作并检索用户的帐户详细信息。
我的问题是我想将用户保存在我的数据库中,所以我需要验证 id_token
在服务器端得到。我尝试在服务器端使用 google API 库执行此操作,如图所示 here ,但我键入了一个无效的 token 结果。我终于决定尝试 tokeninfo
端点 https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=<my_token>
它应该返回包含用户详细信息和其他信息的主体的 200 响应,但我得到主体的 400 响应 {"error_description": "Invalid Value"}
.
我用 jwt.io 解码了 token 以确保它不是过期的 token 并且 token 仍然非常有效。
这是我在 flutter 上用来获取 id_token 的 google_sign_in 代码。
GoogleSignIn _googleSignIn = new GoogleSignIn(
scopes: <String>[
'profile',
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
Future<Null> _handleSignIn() async {
try {
_googleSignIn.signIn().then((result){
result.authentication.then((googleKey){
print(googleKey.accessToken);
print(googleKey.idToken);
print(_googleSignIn.currentUser.displayName);
}).catchError((err){
print('inner error');
});
}).catchError((err){
print('error occured');
});
print('signed in .....');
} catch (error) {
print(error);
}
}
如有任何帮助,我们将不胜感激。
最佳答案
事实证明, token 一直都是有效的。 token 在命令行上被截断并变得无效。
我现在测试有效性的方式是使用HTTP包来测试,不用复制token。
例如。
Dio dio = new Dio();
Response response = await dio.get('https://www.googleapis.com/oauth2/v1/tokeninfo?id_token='+googleKey.idToken);
print(response.data); //contains the token info
关于dart - 在服务器端验证时来自 google_sign_in 插件的 token 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52356151/