已发布LibGDX应用集成Google Play Games Services遇ApiException:4错误求助
解决Google Play Games Services登录ApiException: 4的问题
我之前在集成游戏服务排行榜功能时也碰到过完全一样的错误,结合官方文档和踩过的坑,给你几个针对性的排查方向:
1. 等待Google服务器配置同步
刚在Google Play Console关联应用或修改SHA-1、包名后,Google的服务器可能需要几小时甚至1天的时间完成同步,这期间即使配置都正确也可能触发错误。建议先等待一段时间,再测试登录功能。
2. 核对OAuth 2.0客户端ID的SHA-1配置
前往Google Cloud Console,找到对应应用的OAuth 2.0客户端ID:
- 确保同时添加了调试版和发布版的SHA-1指纹(很多人只加了发布版,但调试时用的是debug签名,导致匹配失败)
- 客户端ID的包名必须和应用的包名完全一致,大小写都不能错
3. 确认测试账号已授权
Google Play Games Services默认只允许授权的测试账号登录:
- 在Google Play Console的游戏服务页面,进入「测试」选项卡
- 添加你的测试邮箱到测试人员列表,并且确保测试账号已经接受了发送到邮箱的邀请
- 注意:测试账号不能是游戏的开发者账号(除非你同时添加为测试人员)
4. 检查Android Studio的签名配置
打开Module级别的build.gradle文件,确认签名配置正确:
android { signingConfigs { release { storeFile file("your-release-keystore.jks") storePassword "your-store-password" keyAlias "your-key-alias" keyPassword "your-key-password" } debug { storeFile file(System.getProperty("user.home") + "/.android/debug.keystore") } } buildTypes { release { signingConfig signingConfigs.release // ...其他配置 } debug { signingConfig signingConfigs.debug // ...其他配置 } } }
确保发布版签名的SHA-1和Google Play Console里的完全一致,调试版用的是默认debug.keystore的SHA-1。
5. 排查登录代码逻辑
检查你的onActivityResult方法是否正确处理了登录结果:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); // 登录成功,初始化游戏服务 Games.getGamesClient(this, account).signIn(); } catch (ApiException e) { Log.e(TAG, "登录失败: " + e.getStatusCode()); // 这里可以根据错误码做对应处理,比如提示用户检查配置 } } }
另外,确保设备上的Google Play Services是最新版本,旧版本可能存在兼容性问题。
附上你提供的错误日志:
com.google.android.gms.common.api.ApiException: 4: at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source) at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source) at com.jdstudio.flappyplane.AndroidLauncher.onActivityResult(AndroidLauncher.java:123) at android.app.Activity.dispatchActivityResult(Activity.java:6241) at android.app.ActivityThread.deliverResults(ActivityThread.java:3591) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3638) at android.app.ActivityThread.access$1300(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5290) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
内容的提问来源于stack exchange,提问作者Michał Woźniak
相关产品推荐
相关产品推荐

