Ragnar
Ragnar

Reputation: 255

How to retain Google Drive authentication when using Intent.ACTION_VIEW in Android?

I am using Google Drive REST api v3 in my app. When the app starts then a user will log into his/her Google account and get authenticated. When user starts this intent

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(googleDriveFileUrl));

And lets say user picks Google Drive app from the launcher, now the sign in dialog pops out and asks to log in again. Is it possible to keep user in with the same account so that the new activity does not ask the log in again?

Upvotes: 0

Views: 303

Answers (1)

Android Enthusiast
Android Enthusiast

Reputation: 4960

There are 2 ways to validate account authentication:

  • No registered account email on the device

GoogleApiClient GAC = new GoogleApiClient.Builder(context) //.setAccountName(email) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(...) .addOnConnectionFailedListener(...) .build();

Google Play Services will pop up the account picker dialog for you to select valid account or to create a new one.

  • Specify valid account's email on the device

GoogleApiClient GAC = new GoogleApiClient.Builder(context) .setAccountName(email) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(...) .addOnConnectionFailedListener(...) .build();

You have to use one of the device's registered ones via account picker:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
...
static final int REQ_ACCPICK = 999;
...
startActivityForResult(AccountPicker.newChooseAccountIntent(null, null,
new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null), REQ_ACCPICK);
...
@Override
protected void onActivityResult(int request, int rslt, Intent data) {
if (
request == REQ_ACCPICK &&
rslt == RESULT_OK && 
data != null && data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) != null
)
email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);

Here's a sample demo app in which discuss require sccount pick: https://github.com/seanpjanson/GDAADemo/blob/master/app/src/main/java/com/spjanson/gdaademo/MainActivity.java

Upvotes: 1

Related Questions