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

1

6

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);
                ...
            }
        });
    }
}
Farver answered 23/8, 2019 at 12:5 Comment(2)
Did you ever find out what is causing this? I have the same problem after upgrading Gradle from 3.1.2 to 3.5.0. It could be related to this warning: "The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.5.0." ? The Android target and build versions are set to 27 - I had them at 28, but that did not solve the problemCroft
The solution indicated here works: issuetracker.google.com/issues/131072620 Include the following in your proguard-rules.pro: -keepclassmembers class * { @ com.google.api.client.util.Key <fields>; }Farver
C
6

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>;
}
Croft answered 12/9, 2019 at 12:14 Comment(2)
This worked for me. Such things should be printed at the very top of the google drive api documentation!Misdemeanant
Nice! Worked for me. After hours of searching... ThanksClasp

© 2022 - 2024 — McMap. All rights reserved.