Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API 403 Forbidden error if i upgrade to Gradle Plugin v3.5.0

I recently updated the Gradle plugin from version 3.4.2 to 3.5.0.
The code to access the list of Drive files has stopped working.
I get: 403 Forbidden domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
if I use 3.4.2 it works perfectly again.

// Login
signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(DriveScopes.DRIVE_FILE))
            .build();
GoogleSignInClient client = GoogleSignIn.getClient(Backup.this, signInOptions);
startActivityForResult(client.getSignInIntent(), REQUEST_CODE_SIGN_IN);



@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == REQUEST_CODE_SIGN_IN) {
        if (resultCode == Activity.RESULT_OK && resultData != null) {
            handleSignInResult(resultData);
        }
        ...
    }
super.onActivityResult(requestCode, resultCode, resultData);
}

private void handleSignInResult(Intent result) {
    GoogleSignIn.getSignedInAccountFromIntent(result)
    .addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() {
        @Override
        public void onSuccess(GoogleSignInAccount googleAccount) {
            // Use the authenticated account to sign in to the Drive service.
            GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(Backup.this, Collections.singleton(DriveScopes.DRIVE_FILE));
            credential.setSelectedAccount(googleAccount.getAccount());
            Drive googleDriveService =
                    new Drive.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new GsonFactory(),
                            credential)
                            .setApplicationName("MyAPP")
                            .build();

            mDriveServiceHelper = new BackupDriveServiceHelper(googleDriveService);

            // get info
            mDriveServiceHelper.readFileInfo()
                .addOnSuccessListener(new OnSuccessListener<Bundle>() {
                    ...
                })
                .addOnFailureListener(new OnFailureListener() {
                    ...
                });
            ...
        }
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        }
    });
}



public class BackupDriveServiceHelper {
    ...
    public Task<Bundle> readFileInfo() {
        return Tasks.call(mExecutor, new Callable<Bundle>() {
            @Override
            public Bundle call() throws Exception {
                // Retrieve the metadata as a File object.
                String pageToken = null;
                do {
                    FileList result = mDriveService.files().list()
                            .setSpaces("drive")
                            .setFields("files(id, name, size, modifiedTime, description)")
                            .setPageToken(pageToken)
                            .execute();     // <<<<< error 403
                    ...
                    pageToken = result.getNextPageToken();
                } while (pageToken != null);
                ...
            }
        });
    }
}
like image 935
Halerq Avatar asked Aug 23 '19 12:08

Halerq


1 Answers

I found that while targetting Android 8 (API27) I could get Google Drive to work with Gradle 3.5.0 if I added this to the project gradle.properties

android.enableD8=false

However, after targetting Android 9 (API 28) and replacing android.support.* with androidx.* I get this warning:

The option 'android.enableD8' is deprecated and should not be used anymore. 

and the problem returns. It seems to me the D8 DEX compiler is causing the problem, but it can no longer be disabled?

I have filed a bug report for D8 https://issuetracker.google.com/issues/140882055

Update: the solution suggested by Google does indeed work: after adding this to proguard-rules.pro Google Drive works with Gradle 5.4.1 and Plugin 3.5.0

-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}
like image 178
Gerrit Beuze Avatar answered Oct 13 '22 07:10

Gerrit Beuze