Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleAccountCredential + Google Play Services + Refresh Token - how this works together?

I have published a pretty successful app about 2 weeks ago. But starting from yesterday, users keep sending me emails about Drive not being accessable anymore. After a quick debug, I found that requests to the Drive API now return "403 Forbidden" -> "Access Not Configured".

I think this might be an issue with the refresh token not being handled properly.

I'm using the following code (from the Android Drive SDK samples):

mCredentials = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
String accountName = PreferenceManager.getDefaultSharedPreferences(this).getString(PREF_DRIVE_NAME, null);
if (accountName != null) {
     setupDrive(accountName);
} else {
     startActivityForResult(mCredentials.newChooseAccountIntent(), 0);
}

setupDrive(...) looks like this:

 mCredentials.setSelectedAccountName(accountName);
 try {
        mCredentials.getToken();
 } catch (Exception e) {
        Log.w(AbstractDriveActivity.class.getSimpleName(), "Error getting auth token", e);
        if (e instanceof UserRecoverableAuthException) {
              Intent intent = ((UserRecoverableAuthException) e).getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_FROM_BACKGROUND);
        startActivity(intent);
        } else {
               Toast.makeText(AbstractDriveActivity.this, getString(R.string.toast_drive_setup_error),
                            Toast.LENGTH_SHORT).show();
               finish();
        }
  }
  drive = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(),
                    mCredentials).build();

Any idea what might be wrong here?

From my understanding, GoogleAccountCredential uses the Google Play Services to manage the OAuth2 flow and all you need to provide is the username. Am I wrong? Did I miss something?

Btw: After clearing app data, selecting the Google Account again, everything works fine. That's why I think that it has something to do with the refresh token.

Goddchen

like image 951
Goddchen Avatar asked Dec 13 '25 16:12

Goddchen


1 Answers

No guaranty, but the problem might come from here:

mCredentials = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);

This method appears as deprecated to me. You should upgrade your SDK and environment and change it to:

mCredentials = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
like image 174
user2574051 Avatar answered Dec 15 '25 07:12

user2574051