onConnectionFailed giving SIGN_IN_REQUIRED(4)

android, google-play, google-play-services

Solution

I had the same problem.

From documentation: The client may choose to continue without using the API or it may call startResolutionForResult(Activity, int) to prompt the user to sign in.

So you should try to sign in by using startResolutionForResult() function

@Override
public void onConnectionFailed(ConnectionResult result) {
   if (result.hasResolution()) {
       try {
           // !!!
           result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
       } catch (SendIntentException e) {
           mPlusClient.connect();
       }
   }

   mConnectionResult = result;
}

Problem

I am developing an Android application where I want to use the Google API. For that I have imported the google-play-service-lib project. I am following this link to initialize GoogleApiClient object. My code: 1) In the `onCreate()` method I am building the GoogleApiClient object: ``` mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API, null) .addScope(Plus.SCOPE_PLUS_LOGIN) .build(); ``` 2) In `onStart()`, I call `mGoogleApiClient.connect()`. 3) My activity implements ConnectionCallbacks and OnConnectionFailedListener. 4) My `onConnectionFailed()` method looks like: ``` public void onConnectionFailed(ConnectionResult result) { //in dubug result looks like : ConnectionResult{ //statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent // {41f8ca70: android.os.BinderProxy@41f8ca10}} try { if(!mIntentInProgress && result.hasResolution()) { mIntentInProgress=true; result.startResolutionForResult(mActivity, RC_SIGN_IN); } } catch (SendIntentException e) { e.printStackTrace(); } } ``` 5) My `onActivityResult()` method contains: ``` if (requestCode == RC_SIGN_IN) { if (!mGoogleApiClient.isConnecting()) { mGoogleApiClient.connect(); } } ``` When I run my app I get a Toast that says that an internal error popped up. I did create the project in the Google console.

Original source