Google Play Games Android Sign In Getting Error Code 4

Viewed 3723

I am currently working on an Android app in which I want to use Google Play Games services (Achievements, Leaderboards, ...). After many iterations I finally got the code working to show the Google Play Games Login promt. The problem is now that after the user signs in it takes a while and then throws error code 4 with error message being empty. After doing a lot of research nothing really helped me solving this problem.

I have tried starting it directly out of Android Studio, installing the app manually or over Google Play, all on multiple devices, nothing worked.

Here's some code if required:

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();
        }
    }
}
5 Answers

Make sure that you use the SHA1 key.

The command "keytool -list -keystore [path to debug keystore]" may display a SHA-256 key.

To get your SHA1 key you can follow these steps:

  1. Open your project
  2. Click on "Gradle" (on the right side)
  3. Choose your module
  4. Tasks -> android -> (double click) signingReport

image: get your SHA1 key

After searching on the internet for hours and checking every step from the troubleshooting section in the google play game services documentation (https://developers.google.com/games/services/android/troubleshooting), I filled out the "OAuth Consent Screen" form and it finally worked.

  1. Log in to the Google Play Console
  2. Go to Game Services
  3. Go to your game
  4. On the Game Details page scroll all the way down to the linked API console project
  5. Click the link to the linked API console project
  6. Click "OAuth Conset Screen" in the left side bar
  7. Choose external
  8. Fill out the form and upload an icon
  9. Send for verification

After that my Google Play Game Services Sign In worked instantly.

Please check the following details for GPG Sign in issues:

Make sure that you have followed the instructions to create your client IDs and configure the games services.

Your meta data Tag in manifest should match with your application's numeric ID, and it should only contain the numbers. Also double check your package name. Do not use the sample app package name. Use your own.

The certificate with which you are signing your game should match the certificate fingerprint associated to your client ID.

The last important thing, Check that test accounts are enabled. I keep forgetting this step.

Make sure that the fingerprint from App signing certificate matches with the linked apps from Game services. You can also verify these details by navigating to your Google API console -> Select your app -> Credentials -> OAuth 2.0 client IDs.

The other thing I noticed that, if the app is released, then you need to install the app from the Play Store for these credentials to work. If the app is not yet released, then make sure you are using the debug fingerprint and that matches in your Games services (Debug fingerprint).

As far as you have these fingerprints in place, you should be fine. If you are still having issues, I would recommend to go through the code lab to get step by step help on how to to extend an existing Unity game to run on an Android device and integrate Play Game Services. https://codelabs.developers.google.com/codelabs/playservices_unity/index.html?index=..%2F..%2Findex#0

Or here are the sample apps to help with quick test - https://github.com/playgameservices/android-basic-samples.

You can also help us with the logcat to better understand the issue.

Recently, I faced the problem when i was trying to write the user data on his drive using the saved game api. And i figured out the problem. Below are my observation hope it will help in general scenario:

  1. You can test the play games api in debug build by adding testers in your play console.
  2. If you want to test on signed apk, then you need to generate the SHA1 key by using your "app.keystore" keystore file and add the android app with this SHA1 key in play game services particular for that app. Now you would be able to get the expected result.(It was the problem in my case)
  3. If you want to store the data on user drive, then you need to enable "saved games" api in play game services.

In my case everything looked fine but get error code, then i check Google Play Console> Play Game Service and publish the changes. The key is publishing things that i made.

Related