【问题标题】:Google Drive API 403 Forbidden error if i upgrade to Gradle Plugin v3.5.0如果我升级到 Gradle Plugin v3.5.0,Google Drive API 403 Forbidden 错误
【发布时间】:2019-08-23 12:05:29
【问题描述】:

我最近将 Gradle 插件从版本 3.4.2 更新为 3.5.0。
访问云端硬盘文件列表的代码已停止工作。
我得到:403 Forbidden domain":"usageLimits"、"reason":"dailyLimitExceededUnreg"、"message":"超过了未经身份验证的使用的每日限制。继续使用需要注册。”
如果我使用3.4.2,它会再次完美运行。

// 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);
                ...
            }
        });
    }
}

【问题讨论】:

  • 你有没有发现是什么原因造成的?将 Gradle 从 3.1.2 升级到 3.5.0 后,我遇到了同样的问题。它可能与此警告有关:“指定的 Android SDK 构建工具版本 (27.0.3) 被忽略,因为它低于 Android Gradle 插件 3.5.0 的最低支持版本 (28.0.3)。” ? Android 目标和构建版本设置为 27 - 我将它们设置为 28,但这并没有解决问题
  • 此处指示的解决方案有效:issuetracker.google.com/issues/131072620 在您的 proguard-rules.pro 中包含以下内容:-keepclassmembers class * { @ com.google.api.client.util.Key ; }

标签: android google-drive-api gradle-plugin


【解决方案1】:

我发现,在以 Android 8 (API27) 为目标时,如果我将其添加到项目 gradle.properties 中,我可以让 Google Drive 与 Gradle 3.5.0 一起使用

android.enableD8=false

但是,在以 Android 9 (API 28) 为目标并将 android.support.* 替换为 androidx.* 后,我收到以下警告:

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

问题又来了。在我看来是 D8 DEX 编译器导致了问题,但它不能再被禁用?

我已提交 D8 的错误报告 https://issuetracker.google.com/issues/140882055

更新:Google 建议的解决方案确实有效:将其添加到 proguard-rules.pro 后,Google Drive 可与 Gradle 5.4.1 和 Plugin 3.5.0 一起使用

-keepclassmembers class * {
  @com.google.api.client.util.Key <fields>;
}

【讨论】:

  • 这对我有用。此类内容应打印在 google drive api 文档的最顶部!
  • 不错!为我工作。经过数小时的搜索......谢谢
猜你喜欢
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
相关资源
最近更新 更多