You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Android应用使用Client ID实现Google登录时遇PlatformException错误

Flutter Android Google登录PlatformException错误(错误码10)

问题描述

在Flutter Android应用中,指定clientId调用Google登录时触发PlatformException错误,但不传入clientId时登录流程可正常运行。已确认clientId正确,Google Cloud项目已配置SHA-1密钥,OAuth2同意屏幕设置符合要求。

核心代码

uploadToGoogleDrive() async {
  try {
    final GoogleSignIn googleSignIn = GoogleSignIn(
        clientId: clientId, scopes: [ga.DriveApi.driveFileScope]);

    final GoogleSignInAccount? googleUser = await googleSignIn.signIn();

    if (googleUser != null) {
      final auth.AuthClient? client =
          await googleSignIn.authenticatedClient();

      final dir = await getApplicationDocumentsDirectory();

      fa.File file = fa.File("${dir.path}/db_backup.isar");
      await isar.copyToFile("${dir.path}/db_backup.isar");

      ga.File fileToUpload = ga.File();
      DateTime now = DateTime.now();

      fileToUpload.name =
          "${now.toIso8601String()}_${p.basename(file.absolute.path)}";

      final drive = ga.DriveApi(client!);

      ga.File x = await drive.files.create(fileToUpload,
          uploadMedia: ga.Media(file.openRead(), file.lengthSync()));

      if (x.id != null) {
        print(x.id!); // 打印Drive中创建的文件ID
        setState(() {
          fileId = x.id!; // 保存到fileId变量
        });
      }

      if (file.existsSync()) {
        file.deleteSync();
      }
    } else {
      if (context.mounted) {
        ScaffoldMessenger.of(context)
            .showSnackBar(const SnackBar(content: Text("登录失败")));
      }
    }
  } catch (e) {
    if (context.mounted) {
      ScaffoldMessenger.of(context)
          .showSnackBar(SnackBar(content: Text(e.toString())));
    }
  }
}

错误信息

ERROR: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)

依赖版本

dependencies:
  googleapis: ^13.2.0
  googleapis_auth: ^1.6.0
  google_sign_in: ^6.2.1
  extension_google_sign_in_as_googleapis_auth: ^2.0.12

已尝试操作

  • 验证clientId正确性;
  • 确认SHA-1密钥已添加至Google Cloud项目;
  • 配置OAuth2同意屏幕。

可能原因及解决方法

1. ClientId类型或配置不匹配

  • 确保使用Android平台专属的clientId,而非Web/iOS版本。Google Cloud控制台中,Android客户端的clientId格式为com.googleusercontent.apps.xxxxxx,需与应用google-services.json(若使用Firebase)中的client_id完全一致;
  • 检查Google Cloud的OAuth 2.0客户端ID配置:包名需与android/app/build.gradle中的applicationId完全匹配,SHA-1需对应当前构建使用的签名密钥。

2. 签名密钥SHA-1不匹配

  • debug模式下,使用keytool -list -v -keystore ~/.android/debug.keystore(Windows路径:C:\Users\<用户名>\.android\debug.keystore)获取debug密钥的SHA-1,确认已添加到Google Cloud;
  • release模式下,确保使用的签名文件SHA-1已在Google Cloud配置,避免debug/release签名混用导致验证失败。

3. AndroidManifest配置补充

  • 在android/app/src/main/AndroidManifest.xml中添加clientId的meta-data配置,替代或配合代码中的clientId设置:
    <meta-data
        android:name="com.google.android.gms.auth.api.client_id"
        android:value="你的Android clientId" />
    

4. OAuth同意屏幕测试用户配置

  • 若OAuth同意屏幕处于测试模式,必须将登录使用的Google账号添加到测试用户列表,否则会触发权限拦截。

5. 缓存与依赖清理

  • 执行flutter clean清理项目缓存,再运行flutter pub get重新拉取依赖;
  • 尝试升级google_sign_in到最新稳定版,修复版本兼容性问题。

内容的提问来源于stack exchange,提问作者Cipher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:55:56