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

Flutter Android端Google登录报错PlatformException(ApiException:10)求助

Flutter Google登录Android端ApiException:10 解决方案

核心原因

ApiException:10 本质是Android端OAuth配置不匹配,常见于签名指纹(SHA1)、客户端ID、Manifest配置三者不一致,结合你iOS端正常的情况,重点排查Android专属配置。

分步解决

1. 区分客户端ID(关键错误点)

你的代码中clientId和serverClientId使用了同一个值,这是错误的:

  • 在GCP控制台需创建两个客户端ID:
    • Android类型:给Flutter Android端使用,对应代码中的clientId
    • Web应用类型:给后端验证ID Token使用,对应代码中的serverClientId
  • 修改后的代码示例:
    const List<String> scopes = <String>[
      "https://www.googleapis.com/auth/userinfo.email",
    ];
    
    final androidClientId = "你的Android客户端ID"; // 格式:xxx-xxx.apps.googleusercontent.com
    final webClientId = "你的Web客户端ID";
    
    final googleSignIn = GoogleSignIn(
      scopes: scopes,
      clientId: androidClientId,
      serverClientId: webClientId,
    );
    

2. 配置AndroidManifest.xml

Android端需要添加URI Scheme和必要的元数据,在android/app/src/main/AndroidManifest.xml的<application>标签内插入:

<!-- Google Play Services版本元数据 -->
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

<!-- Google登录跳转Activity -->
<activity
    android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity"
    android:excludeFromRecents="true"
    android:exported="false" />

<!-- URI Scheme:替换为你的Android客户端ID的反向格式 -->
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- 示例:com.googleusercontent.apps.1234567890abcdefghijklmnopqrstuvwxyz -->
    <data android:scheme="com.googleusercontent.apps.你的Android客户端ID前缀" />
</intent-filter>

注:Android客户端ID格式为123456-abcdef.apps.googleusercontent.com,取com.googleusercontent.apps.123456abcdef作为scheme。

3. 重新验证Debug SHA1指纹

模拟器使用debug签名,必须确保GCP中配置的SHA1是debug keystore的指纹:

  • 获取debug SHA1的命令:
    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    
  • 复制输出的SHA1值(无空格、大小写一致),更新到GCP控制台的Android客户端ID配置中。

4. 检查OAuth同意屏幕配置

  • 如果同意屏幕处于测试状态,必须将你测试用的Google邮箱添加到「测试用户」列表
  • 确保同意屏幕的「范围」包含你代码中使用的userinfo.email

5. 彻底清理重建项目

flutter clean
rm -rf android/app/build
flutter run

额外排查点

  • 确认Android模拟器已安装Google Play Services,且版本不低于google_sign_in包依赖的版本
  • 检查GCP项目中是否启用了「Google Identity API」

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 07:19:55