在 Flutter 中使用 google_sign_in 时获取 idToken 'null'

Getting idToken 'null' when using google_sign_in in Flutter

我正在使用 google_sign_in 插件。

它运行良好。但我有一个问题。

我需要将 idToken 发送到后端服务器以验证用户。

但是 google 登录响应中的 IdToken 为空。

代码简单

  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth =
      await googleUser.authentication;
  print("Printing google user info");
  print(googleAuth.accessToken);
  print(googleAuth.idToken);
  print(googleUser.displayName);

其他属性具有正确的值。但是 idToken 为 null

我google为此而说我需要网络客户端 ID。

我也是

final _googleSignIn = GoogleSignIn(clientId: webClientId);

你们能帮忙吗?

我是不是漏掉了什么?

在尝试对用户进行身份验证之前,您必须像这样指定范围

GoogleSignIn _googleSignIn = GoogleSignIn(
            scopes: [
                'email',
                'https://www.googleapis.com/auth/contacts.readonly',
                "https://www.googleapis.com/auth/userinfo.profile"
            ],
        );

然后您可以请求用户进行身份验证。 获取 isToken 调用此方法

GoogleSignInAuthentication googleSignInAuthentication = await result.authentication;

如果您在云控制台中正确配置了项目,您应该可以毫无问题地获取 ID

我面对这个问题两天了,终于。 (在我的例子中)这个解决方案有效。

https://firebase.flutter.dev/docs/installation/android#installing-your-firebase-configuration-file

  • 添加 com.google.gms:google-services 作为 android/build.gradle

    内部的依赖项
      dependencies {
        // ... other dependencies
        classpath 'com.google.gms:google-services:4.3.8'
      }
    }
    
    
  • /android/app/build.gradle

    中添加apply plugin: 'com.google.gms.google-services'
    apply plugin: 'com.google.gms.google-services'