1

I tried 1 day ago to sign in on google play games but never unless and I have got below message.

Logcat

I/salahtaha: com.google.android.gms.common.api.ApiException: 4: 4: 

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.salah250.test.test">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Java Code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private void signInSilently() {
        GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
                GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
        signInClient.silentSignIn().addOnCompleteListener(this,
                new OnCompleteListener<GoogleSignInAccount>() {
                    @Override
                    public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                        if (task.isSuccessful()) {
                            GoogleSignInAccount signedInAccount = task.getResult();
                            Toast.makeText(MainActivity.this, "a", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.i("salahtaha",String.valueOf(task.getException()));
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        signInSilently();
    }

}

I added game on google play console using game services and It's linked with firebase project.

Note : SHA1 in my app identical in my firebase

Taha Sami
  • 1,565
  • 1
  • 16
  • 43

1 Answers1

5

Based on the CommonStatusCodes from Google API for Android

Looking at the API Exception, the status code 4 is provided which is linked with the following information from Google's API for Android documentation.

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. Alternately, if hasResolution() returns true the client may call startResolutionForResult(Activity, int) to prompt the user to sign in. After the sign in activity returns with RESULT_OK further attempts should succeed.

Also, the method which you are calling SignInSliently, I've also found the following information:

  • (void) signInSilently
    Attempts to sign in a previously authenticated user without interaction.
    The delegate will be called at the end of this process indicating success or failure.

Therefore, I believe that you are trying to sign in while not having any previous record of authenticated sign in. In order to use the signInSilently, you must manually sign into the service.

Credit to noktigula for providing the source of these documentation in the APIException question which contributes to the same error on this question.

Nero
  • 1,058
  • 1
  • 9
  • 25
  • Therefore, I believe that you are trying to sign in while not having any previous record of authenticated sign in. In order to use the signInSilently, you must manually sign into the service. || I don't understand this point – Taha Sami Dec 27 '18 at 10:45
  • From what I've found, signInSliently will hide the login dialogue and login to the service itself if the user has already signed in previously. If not, it will return the error code of 4. In your code, if this code is returned, you should request the user to sign in again. – Nero Dec 27 '18 at 11:07
  • In fact I can't understand this matter It seems complicated first I want to add normal google login button to gmail or what ? – Taha Sami Dec 27 '18 at 11:11
  • That is correct. Let's try to clear out the confusion. A user should login normally (they have to provide the username + password). From there, you should be able to use this function which will log the user into the service again without having the need for the user to provide their credentials again.
    If the user is not logging into the services manually, signInSilently will always fail as it's unable to find the last successful authentication.
    – Nero Dec 27 '18 at 11:15
  • But why the another games not has normal login button (gmail) ? – Taha Sami Dec 27 '18 at 11:22
  • + when I try put leaderboard button I get error message It's show me GoogleSignInAccount must not be null. || so the result I wanna add google login button (gmail) ? – Taha Sami Dec 27 '18 at 11:22
  • If you want to have this discussion, please message me on discord (available on my profile bio) as comments are not a delegated for these sort of communication. Plus, your question is outside of the context of the original question which may need another question raised. At this moment, I was only trying to solve the reason for the APIException. I believe every game has a sign in method in place (at least at the start of the first launch). Like I said, I am more than happy to continue this conversation on Discord. – Nero Dec 27 '18 at 11:27