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:
Now I close my app and run again.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With