【问题标题】:Invalid token from google_sign_in plugin when verifying on server side在服务器端验证时来自 google_sign_in 插件的令牌无效
【发布时间】:2019-02-20 16:39:38
【问题描述】:

请我尝试使用插件google_sign_in plugin 在我的颤振应用程序上实现社交身份验证。它在客户端正常工作并检索用户的帐户详细信息。
我的问题是我想将用户保存在我的数据库中,所以我需要验证在服务器端获得的id_token。我尝试使用服务器端的 google API 库执行此操作,如 here 所示,但我得到了无效的令牌结果。我终于决定尝试tokeninfo端点https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=<my_token>,它应该返回包含用户详细信息和其他信息的正文的200响应,但我收到正文{"error_description": "Invalid Value"}的400响应。
我使用 jwt.io 对令牌进行了解码,以确保它不是过期的令牌并且令牌仍然非常有效。

这是我在 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);
    }
  } 

任何帮助将不胜感激。

【问题讨论】:

  • 同样的问题,你找到答案了吗?
  • 不。当我创建一个不同的项目时工作正常。仍然无法说出确切的原因。
  • @AntonShkurenko 我的问题是从命令行复制代码
  • 你找到解决办法了吗?

标签: dart flutter google-signin


【解决方案1】:

在文档 google_sign_in 中,他们有 IOS 的详细信息,但在 Android 中,他们缺少这些步骤:

  1. 添加

类路径'com.google.gms:google-services:4.2.0'

在你的根目录“build.gradle”中的依赖项

  1. 添加

应用插件:'com.google.gms.google-services'

在你的“app/build.gradle”底部

  1. 添加

实施“com.google.gms:google-services:4.2.0”

在“app/build.gradle”的依赖项中

  1. 转到您的 Firebase 控制台,下载

google-services.json

并将其复制到目录“app/”

完成所有这些步骤后,您可以运行并获取 idToken 正常

【讨论】:

    【解决方案2】:

    事实证明这些令牌一直都是有效的。令牌在命令行上被截断并变为无效。
    我现在测试有效性的方式是使用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
    

    【讨论】:

    • 那么如何获取整个 id_token 我正在为此苦苦挣扎,是的,当使用 googleapis 添加令牌时返回响应但仍然在我的 API URL localhost 中返回 false
    猜你喜欢
    • 2015-03-03
    • 2023-01-12
    • 2017-06-10
    • 2017-12-28
    • 2017-10-30
    • 1970-01-01
    • 2015-10-19
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多