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

Flutter非Firebase环境下谷歌登录失败(错误码10)求助

Flutter谷歌账号登录失败(错误码10)- 非Firebase、未发布Play Store场景

问题背景

  • 未集成Firebase,处于开发初期,未发布至Play Store
  • 核心需求:用户登录后上传照片至个人Google Drive
  • 实现代码:
import 'dart:developer';

import 'package:google_sign_in/google_sign_in.dart';

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: <String>[
    'https://www.googleapis.com/auth/drive.file',
  ],
);

class AuthManager {
  static Future<GoogleSignInAccount?> signIn() async {
    try {
      final account = await _googleSignIn.signIn();
      log('account: ${account?.toString()}', name: 'AuthManager');
      return account;
    } catch (error) {
      log('signIn error:', name: 'AuthManager');
      log(error.toString(), name: 'AuthManager');
      return null;
    }
  }

  static Future<void> signOut() async {
    try {
      _googleSignIn.disconnect();
    } catch (error) {
      print(error);
    }
  }

  static Future<bool> isSignedIn() async {
    return _googleSignIn.isSignedIn();
  }
}
  • 错误日志:
[AuthManager] signIn error:
[AuthManager] PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)

已尝试操作

  • 生成keystore并在debug模式下签名应用,问题未解决
  • 使用包名和keystore的SHA1创建OAuth客户端,但仍报错,不清楚Flutter如何关联该凭证

解决方案

错误码10属于开发者配置问题,针对你的场景按以下步骤排查:

  1. 确保OAuth客户端类型正确
    必须创建「Android客户端」类型的OAuth凭证,不要选择Web、iOS或其他类型,否则无法匹配Android端的登录请求。

  2. 在Flutter中关联OAuth客户端ID

    • 打开Android项目的android/app/src/main/res/values/strings.xml,添加客户端ID:
      <string name="server_client_id">你的Android OAuth客户端ID</string>
      
    • 初始化GoogleSignIn时显式指定clientId参数:
      GoogleSignIn _googleSignIn = GoogleSignIn(
        scopes: <String>[
          'https://www.googleapis.com/auth/drive.file',
        ],
        clientId: "你的Android OAuth客户端ID", // 新增该行
      );
      
  3. 验证SHA1和包名完全匹配

    • 确认用于生成OAuth凭证的SHA1,是当前debug签名用的keystore对应的SHA1。如果使用默认debug keystore,可通过以下命令获取:
      keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
      
    • 包名必须和android/app/build.gradle中defaultConfig下的applicationId完全一致,大小写、符号都不能出错。
  4. 启用必要的Google Cloud API
    登录Google Cloud控制台,确保项目已启用Google Drive API和Google Sign-In API,未启用的话登录请求会被拦截。

  5. debug模式的额外检查
    如果同时使用自定义keystore和默认debug keystore,建议把两个keystore的SHA1都添加到同一个Android OAuth客户端中,避免切换签名时出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 10:13:14