首页 > 解决方案 > Google Play Games Android 登录出现错误代码 4

问题描述

我目前正在开发一个 Android 应用程序,我想在其中使用 Google Play 游戏服务(成就、排行榜等)。经过多次迭代,我终于得到了显示Google Play Games Login promt的代码。现在的问题是,用户登录后需要一段时间,然后抛出错误代码 4,错误消息为空。在做了很多研究之后,没有什么能真正帮助我解决这个问题。

我试过直接从 Android Studio 启动它,手动或通过 Google Play 安装应用程序,所有这些都在多个设备上,没有任何效果。

如果需要,这里有一些代码:

private static GoogleSignInClient mGoogleSignInClient;

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    mGoogleSignInClient = GoogleSignIn.getClient(this, new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).requestIdToken(getResources().getString(R.string.app_id)).build());

    signInSilently(); // Can't be tested yet.
    // isSignedIn() and loginDeclined are both false
    if(!isSignedIn() && !loginDeclined) startSignInIntent();
}

...

private void startSignInIntent() {
    startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
    loginDeclined = true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    // resultCode = 0

    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task =
                GoogleSignIn.getSignedInAccountFromIntent(intent);

        try {
            GoogleSignInAccount account = task.getResult(ApiException.class);
            // Now this never happens :(
            System.out.println("SUCCESS!");
        } catch (ApiException apiException) {
            System.out.println(apiException.getStatusCode() + " + " + apiException.getMessage());
            // statusCode is 4, message is "4: "

            String message = apiException.getMessage();
            if (message == null || message.isEmpty()) {
                message = "Error signing in to Google Play Games";
            }

            new android.app.AlertDialog.Builder(this)
                    .setMessage(message)
                    .setNeutralButton(android.R.string.ok, null)
                    .show();
        }
    }
}

标签: javaandroidgoogle-play-games

解决方案


确保使用SHA1密钥。

命令“keytool -list -keystore [path to debug keystore] ”可能会显示一个SHA-256密钥。

要获取您的SHA1密钥,您可以按照以下步骤操作:

  1. 打开你的项目
  2. 点击“ Gradle ”(右侧)
  3. 选择您的模块
  4. 任务 -> android ->(双击)signingReport

图片:获取您的 SHA1 密钥


推荐阅读