Kiryl Tkach
Kiryl Tkach

Reputation: 3614

Google Play Games API returns SIGN_IN_REQUIRED

I'm trying to implement automating player sign in to Google Play games in my Android app. Firstly, as mentioned here, I try to sign in silently:

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

private void signInSilently() {
    mGoogleSignInClient.silentSignIn().addOnCompleteListener(this, task -> {
        if (task.isSuccessful())
            //everything ok
        else {
            final ApiException exception = (ApiException) task.getException();
            if (BuildConfig.DEBUG)
                Log.d(TAG, "Silent Sign In failure: ", exception);
            if (exception.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED)
                startSignInIntent();
        }
    });

Every time I got an exception with code 4 (CommonStatusCodes.SIGN_IN_REQUIRED). So in this case I try to sign in with ui:

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

@Override
protected void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);
    if (request == RC_SIGN_IN) {
        final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
            // everything is ok, get account from result
        } else if (result.getStatus().hasResolution()) {
            resolveManually(result.getStatus());
        } else {
            String message = result.getStatus().getStatusMessage();
            if (BuildConfig.DEBUG)
                Log.d(TAG, "status code" + result.getStatus().getStatusCode());
            if (message == null || message.isEmpty()) {
                message = "other error";
            }
            new AlertDialog.Builder(this).setMessage(message)
                    .setNeutralButton(android.R.string.ok, null).show();
        }
    }
}

And here everytime I get message with other error! The status code is again 4 (CommonStatusCodes.SIGN_IN_REQUIRED). How can I get this code when I try to sign in using intent? So, my app are in infinite loop because onResume is called everytime my activity loads after receiving a result, and everytime the status code is CommonStatusCodes.SIGN_IN_REQUIRED. So, where is the problem? In Google samples there is no information how can I handle automatic sign in, only manual with sign in buttons. But google recommends to use automating sign in. Please help anybody to understand what is wrong here.

Upvotes: 1

Views: 1152

Answers (2)

Kiryl Tkach
Kiryl Tkach

Reputation: 3614

There was wrong OAuth 2.0 client ID for the debug version of my app! Don't know why there is SIGN_IN_REQUIRED status code in this situation, it is really confusing!

Upvotes: 0

MrStahlfelge
MrStahlfelge

Reputation: 1791

You must not start the login screen from your onResume method. It is a silent login which works if the user wants it (by tapping a button). That's why the examples show it only this way.

Upvotes: 1

Related Questions