Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleApiClient connect always fails first time but gets successful second time

I have a google plus login set up for app through GoogleApiClient.

Whenever the app is installed first time and tries to make connection through GoogleApiClient it never gets successful and always end up at onConnectionFailed with result containing:

ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4130e760: android.os.BinderProxy@4130e700}}

But when second time login its called it gets successful and onConnected hits. Why is that is it possible to make it successful over the first attempt?

Is there something wrong in my Builder parameters?

public void connectGoogleApi() {
    mGoogleApiClient = new GoogleApiClient.Builder(mainAppContext).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN).build();
    mGoogleApiClient.connect();
}

public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;
        resolveSignInError();
    }

}
like image 340
Maven Avatar asked Jan 14 '15 19:01

Maven


2 Answers

As official docs says here:

If you are using GoogleApiClient to connect to APIs that require authentication, like Google Drive or Google Play Games, there's a good chance your first connection attempt will fail and your app will receive a call to onConnectionFailed() with the SIGN_IN_REQUIRED error because the user account was not specified.

like image 154
Tinko Avatar answered Oct 19 '22 23:10

Tinko


I had the same problem, calling 'connect()' again, this time inside 'onConnected' method fixed it. Strange.

@Override
   public void onConnected(final Bundle arg0) {
   Logger.log("On connected");
   DevicePreferences.getGoogleApiClient().connect();
}
like image 30
Piotr Niewinski Avatar answered Oct 20 '22 00:10

Piotr Niewinski