如何正确设置 google 在 Android 中播放登录?

How to properly setup google play signin in Android?

我在 android 开发了一款游戏。我尝试实施 GooglePlay 登录,但它显示错误。我无法调试此错误。我尝试在模拟器以外的不同 phone 模型中安装该应用程序。

代码:

public void startSignInIntent() {     
    startActivityForResult(mGoogleSignInClient.getSignInIntent(), RC_SIGN_IN);
}

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if (requestCode == RC_SIGN_IN) {

            Task<GoogleSignInAccount> task =
                    GoogleSignIn.getSignedInAccountFromIntent(intent);

            try {
                GoogleSignInAccount account = task.getResult(ApiException.class);
            } catch (ApiException apiException) {
                String message = apiException.getMessage();
                if (message == null || message.isEmpty()) {
                    message = getString(R.string.signin_other_error);
                }


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

        }
        super.onActivityResult(requestCode, resultCode, intent);
    }

编辑

现在,按照建议的方法操作后,登录对话框会立即关闭,不会显示任何错误。

ApiException 的消息不是很清楚:

String message = apiException.getMessage();

要调试它,ApiException 的状态会有所帮助:

int StatusCode = apiException.getStatusCode();

documentation;它可能与 oAuth2 范围有关;但是问题缺少构造 GoogleSignInClient 的代码(可能与重现错误有关)。

另一个怀疑是丢失或过时的 google-services.json. It needs to have the matching (debug or release or both) key fingerprints added, of the keys used to sign that APK package. Even if not using Firebase, one has to setup the project there, in order to add the key fingerprints and to obtain that one config file (there has to be some google_app_id in the string resources and this is exactly what the Play Services Plugin 会从该配置文件中生成。


状态 4 表示 SIGN_IN_REQUIRED:

The client attempted to connect to the service but the user is not signed in.

The client may choose to continue without using the API.

google_app_id 的身份验证失败提示或密钥指纹不匹配。认为不需要许可就可以使用 GoogleSignInClient 获取当前登录的 Google 帐户...所以这可能意味着 API 客户端未登录 Google 播放.