Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleAccountCredential reuse auth - Google Drive

I'm using this method to auth to Google Drive: https://developers.google.com/drive/quickstart-android

Choosing account is working great. Now I want to store user credentials in prefs. I can save account name and then resore it. I want to reuse credentials for future use.

Is it possible to reauth using Google Play Services and GoogleAccountCredential?

This is my scenario:

  1. StartActivity
  2. Search for savedCredentials
  3. If NOT -> show ACCOUNT PICKER (this is working)
  4. If I choose account - saveCredentians to preferences.

Now I close my app and run again.

  1. StartActivity
  2. Search for savedCredentials
  3. YES - there are saved credentials. I want to use them.

I'm trying something like this:

credential = GoogleAccountCredential.usingOAuth2(GoogleDriveBackup.this, DriveScopes.DRIVE_FILE);

//use saved account (stored[0] is OK)
credential.setSelectedAccountName(stored[0]);
credential.getToken();
service = getDriveService(credential);

Where:

 private Drive getDriveService(GoogleAccountCredential credential) {
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), 
new GsonFactory(), credential).build();
      }

This isn't working. I'm getting 500 Internal Server errors from GoogleJsonResponseExcelption. What's the best way to do that using Google Play Services for Android.

like image 742
adek Avatar asked Jan 13 '13 19:01

adek


1 Answers

If what you are trying to do is avoiding that the user has to re-authorize your app then you don't need to save the credential Object. All yo need to do is save the Account the user chose using the account picker.

When re-creating a credential object the next time your app is used, the play services will know that the user has already granted you access before and you won't have to show the auth Intent (available in the Exception thrown).

You do need to provide a way for the user to change the account he chose though.

like image 106
Nicolas Garnier Avatar answered Nov 19 '22 18:11

Nicolas Garnier