'How to properly setup google play signin in Android?

I developed a game in android. I tried to implement the GooglePlay SignIn but it shows an error. I'm not able to debug this error. I tried installing the app in different phone models other than emulators.

Code:

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);
    }

It Loads the GP Games

Then this happens

EDIT

Now after following the suggested methods, the SignIn dialogue closes immediately without showing any errors.



Solution 1:[1]

The message of the ApiException is not really telling:

String message = apiException.getMessage();

To debug this, the status of the ApiException would rather be helpful:

int StatusCode = apiException.getStatusCode();

See the documentation; It could have to do with oAuth2 scopes; but the question lacks the code where the GoogleSignInClient is being constructed (might be relevant to reproduce the error).

Another suspicion would be a missing or outdated 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 would generate from that config file).


Status 4 means 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.

A failed authentication hints for google_app_id or the key fingerprints not matching. Think one does not require permission to get the currently signed-in Google account with the GoogleSignInClient... so this likely means, the API client not being signed in to Google Play.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community