First, you need to start one intention to get login credentials
if(mGoogleApiClient == null){ mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .enableAutoManage(this, this) .addScope(Drive.SCOPE_FILE) .addScope(Drive.SCOPE_APPFOLDER) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(AppIndex.API) .build(); } Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, DRIVE_SIGNIN_RESULT);
No need to connect mGoogleApiClient here you can create a GoogleSignInResult object from the intent information you received in onActivityResult
if (resultCode == RESULT_OK) { switch (requestCode) { case DRIVE_SIGNIN_RESULT: GoogleSignInResult signInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(signInResult); break; } }
Connect mGoogleApiClient now
private void handleSignInResult(GoogleSignInResult result) { if (result.isSuccess()) { GoogleSignInAccount account = result.getSignInAccount(); mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL); } }
source share