@Override protected void onCreate ( Bundle savedInstanceState ) { .. //Here unlike Google plus sign in the serverToken is just the clientId of the web application GoogleSignInOptions gso = new GoogleSignInOptions . Builder ( GoogleSignInOptions . DEFAULT_SIGN_IN ) . requestIdToken ( serverToken ). requestServerAuthCode ( serverToken ). requestEmail (). requestScopes ( Plus . SCOPE_PLUS_LOGIN , Plus . SCOPE_PLUS_PROFILE ) . build (); mGoogleApiClient = new GoogleApiClient . Builder ( this ) . addConnectionCallbacks ( this ) . addOnConnectionFailedListener ( this ). addApi ( Auth . GOOGLE_SIGN_IN_API , gso ). addApi ( Plus . API ) . build (); } protected void onResume () { super . onResume (); //Here isConnected is just a flag that checks whether user is connected to internet. /*To avoid execution of this block you can check whether user previously signedIn on this device by storing a userIdToken and checking whether user needs to be signedIn automatically or not. */ if (! mGoogleApiClient . isConnecting () && ! mGoogleApiClient . isConnected () && isConnected ) { mGoogleApiClient . connect ( GoogleApiClient . SIGN_IN_MODE_OPTIONAL ); } //Here isSignedIn is a boolean flag that tracks whether the user is signedIn or not. else if ( isConnected && mGoogleApiClient . isConnected ()&&! isSignedIn ){ signInUsingNewAPI (); } } private void signInUsingNewAPI () { if (! isSignedIn && isConnected ) { attemptSilentSignIn (); } } private void attemptSilentSignIn (){ OptionalPendingResult < GoogleSignInResult > opr = Auth . GoogleSignInApi . silentSignIn ( mGoogleApiClient ); if ( opr . isDone ()) { // If the user's cached credentials are valid, the OptionalPendingResult will be "done" // and the GoogleSignInResult will be available instantly. Log . d ( TAG , "Got cached sign-in" ); GoogleSignInResult result = opr . get (); handleSignInResult ( result ); } else { // If the user has not previously signed in on this device or the sign-in has expired, // this asynchronous branch will attempt to sign in the user silently. Cross-device // single sign-on will occur in this branch. showProgressDialog (); opr . setResultCallback ( new ResultCallback < GoogleSignInResult >() { @Override public void onResult ( GoogleSignInResult googleSignInResult ) { hideProgressDialog (); handleSignInResult ( googleSignInResult ); } }); } } private void handleSignInResult ( GoogleSignInResult result ){ if (! result . getStatus (). isSuccess ()) { isSignedIn = false ; mIntentInProgress = false ; if ( result . getStatus (). hasResolution ()|| result . getStatus (). getStatusCode ()== CommonStatusCodes . SIGN_IN_REQUIRED ){ freshSignIn (); //Rather than using startResolutionForResult, we invoke our method which attempts to do a fresh sign in and if there is error it is handled in onActivityResult method. } } else { mIntentInProgress = false ; isSignedIn = true ; final GoogleSignInAccount account = result . getSignInAccount (); //Maybe save this result. SharedPreferences . Editor editor = preferences . edit (); editor . putString ( "client_id_token" , account . getIdToken ()); editor . putString ( "auth_code" , account . getServerAuthCode ()); editor . apply (); //You can pass these credentials to your server from here. //Invoke the GPlus People API Plus . PeopleApi . load ( mGoogleApiClient , account . getId ()). setResultCallback ( new ResultCallback < People . LoadPeopleResult >() { @Override public void onResult ( @NonNull People . LoadPeopleResult loadPeopleResult ) { Person person = loadPeopleResult . getPersonBuffer (). get ( 0 ); //Method that obtains the userInfo getProfileInfo ( person , account . getEmail ()); } }); } } private void freshSignIn (){ Intent signInIntent = Auth . GoogleSignInApi . getSignInIntent ( mGoogleApiClient ); showProgressDialog (); startActivityForResult ( signInIntent , RC_SIGN_IN ); } @Override protected void onActivityResult ( int requestCode , int responseCode , Intent intent ) { if ( requestCode == RC_RESOLVE_ERROR ){ mIntentInProgress = false ; if ( responseCode != RESULT_OK ) { isSignedIn = false ; //Maybe show a dialog to user ? return ; } //Attemp connection again. if (! mGoogleApiClient . isConnecting ()) { mGoogleApiClient . connect (); } } else if ( requestCode == RC_SIGN_IN ) { hideProgressDialog (); GoogleSignInResult result = Auth . GoogleSignInApi . getSignInResultFromIntent ( intent ); handleSignInResult ( result , false ); } } // Connection callbacks @Override public void onConnected ( Bundle bundle ) { if (! isSignedIn ) { signInUsingNewAPI (); } } @Override public void onConnectionSuspended ( int i ) { isSignedIn = false ; if (! isSignedIn && isConnected ) { mGoogleApiClient . connect ( GoogleApiClient . SIGN_IN_MODE_OPTIONAL ); } } @Override public void onConnectionFailed ( ConnectionResult result ) { if (! result . hasResolution ()) { GoogleApiAvailability . getInstance (). getErrorDialog ( this , result . getErrorCode (), RC_SIGN_IN ). show (); return ; } if (! mIntentInProgress ) { // Store the ConnectionResult for later usage mConnectionResult = result ; if (! isSignedIn ) { // The user has already clicked 'sign-in' so we attempt to // resolve all // errors until the user is signed in, or they cancel. resolveSignInError (); } } } /** * Method to resolve any signin errors */ private void resolveSignInError () { if ( mConnectionResult . hasResolution ()) { try { mIntentInProgress = true ; mConnectionResult . startResolutionForResult ( this , RC_RESOLVE_ERROR ); } catch ( IntentSender . SendIntentException e ) { mIntentInProgress = false ; mGoogleApiClient . connect ( GoogleApiClient . SIGN_IN_MODE_OPTIONAL ); } } } //Sign out and revoke methods /** * Sign-out from google */ public void signOutFromGoogle () { if ( mGoogleApiClient . isConnected ()) { Auth . GoogleSignInApi . signOut ( mGoogleApiClient ). setResultCallback ( new ResultCallback < Status >() { @Override public void onResult ( Status status ) { isSignedIn = false ; //do other stuff here. mGoogleApiClient . disconnect (); //Builds a fresh instance of GoogleApiClient buildGoogleApiClient (); } }); } } /** * Revoking access from google */ public void revokeGplusAccess () { if ( mGoogleApiClient . isConnected ()) { Auth . GoogleSignInApi . revokeAccess ( mGoogleApiClient ). setResultCallback ( new ResultCallback < Status >() { @Override public void onResult ( Status status ) { isSignedIn = false ; //do other stuff here. mGoogleApiClient . disconnect (); //Builds a fresh instance of GoogleApiClient buildGoogleApiClient (); //You can inform your server of this change } }); } } //Other utility methods private void hideProgressDialog () { if ( mProgressDialog != null && mProgressDialog . isShowing ()) { mProgressDialog . hide (); } } private void showProgressDialog () { if ( mProgressDialog == null ) { mProgressDialog = new ProgressDialog ( this ); mProgressDialog . setMessage ( "Signing In" ); mProgressDialog . setIndeterminate ( true ); } mProgressDialog . show (); }