在 Android 中迁移到最新的 Google Drive API 时出错 - 已超出未经身份验证使用的每日限制。继续使用需要注册

Har*_*789 4 android oauth-2.0 google-drive-api google-drive-android-api

应用程序发布在 Play 商店中,它使用“应用程序数据文件夹”使用Drive API进行备份-恢复。一切正常。但是,根据 Google 的公告,该 API 即将在 2019 年 12 月 6 日被拒绝。因此,为了支持现有用户,我一直根据迁移指南和官方示例应用程序迁移到最新的 API 。

我可以使用下面的代码(来自官方链接)成功进行身份验证。

GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                                         .requestEmail()
                                         .requestScopes(new Scope(DriveScopes.DRIVE_APPDATA))
                                         .build();
GoogleSignInClient client = GoogleSignIn.getClient(this, signInOptions);

// The result of the sign-in Intent is handled in onActivityResult.
startActivityForResult(client.getSignInIntent(), REQUEST_CODE_SIGN_IN);
Run Code Online (Sandbox Code Playgroud)

我也在使用正确的范围 - DriveScopes.DRIVE_APPDATA如官方文档中所述。

我还在里面看到了“电子邮件”和“授予范围”的正确值 onActivityResult()

if (requestCode == REQUEST_CODE_SIGN_IN && resultCode == RESULT_OK) {

   GoogleSignIn.getSignedInAccountFromIntent(data).addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() {
                @Override
                public void onSuccess(GoogleSignInAccount googleSignInAccount) {

                    Log.e("TAG", "Email - " + googleSignInAccount.getEmail()); // prints correct value
                    Log.e("TAG", "Granted scopes - " + googleSignInAccount.getGrantedScopes()); // prints correct value

                    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(getActivity(), Collections.singleton(DriveScopes.DRIVE_APPDATA));
                    credential.setSelectedAccount(googleSignInAccount.getAccount());

                    Drive googleDriveService = new Drive.Builder(
                            AndroidHttp.newCompatibleTransport(),
                            new GsonFactory(),
                            credential)
                            .setApplicationName("App Name") // Changed it for now
                            .build();
                    mDriveServiceHelper = new DriveServiceHelper(googleDriveService);

                    queryFiles();
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试queryFiles()使用下面的代码(来自官方链接)访问备份文件时,

FileList files = driveService.files().list()
    .setSpaces("appDataFolder")
    .setFields("nextPageToken, files(id, name)")
    .setPageSize(10)
    .execute();
for (File file : files.getFiles()) {
  System.out.printf("Found file: %s (%s)\n",
      file.getName(), file.getId());
}
Run Code Online (Sandbox Code Playgroud)

它抛出以下错误

{
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
Run Code Online (Sandbox Code Playgroud)

请帮我修复错误。我相信,由于发布的版本一切正常,因此在 Google API 控制台上的配置方面一切都应该是正确的。

Cmo*_*ery 6

我可能无法找到所有有此问题的线程,但我可以尝试提供一些帮助。

主要答案

如果你和我一样使用 ProGuard。ProGuard 可能会导致在查询期间发生此错误。我使用以下方法修复了它。

# Fix OAuth Drive API failure for release builds
-keep class * extends com.google.api.client.json.GenericJson { *; }
-keep class com.google.api.services.drive.** { *; }
-keepclassmembers class * { @com.google.api.client.util.Key <fields>; }
Run Code Online (Sandbox Code Playgroud)

次要答案

请注意,您不要需要使用钥匙/使用了Android的驱动其余API令牌就像你可以从其他的解决方案找到(它可能不会伤害任何,但它可以)。它与人们在其他地方谈论的内容不符(在这里他们不知道他们在谈论什么)。

有关更多信息,请参阅此处的笔记:通过 OAuth 发布版本的 Google Drive 接收 dailyLimitExceededUnreg

如果您在调试版本中遇到问题,那么您没有正确执行所有操作。我的笔记应该能让你走上正轨。

如果您需要进一步的帮助,我可能会提供帮助,因为这太疯狂了。

使用新 API 从 Drive 获取文件的示例

只需从以下链接中查看

public Task<Pair<String, String>> readFile(String fileId)
Run Code Online (Sandbox Code Playgroud)

https://github.com/gsuitedevs/android-samples/blob/master/drive/deprecation/app/src/main/java/com/google/android/gms/drive/sample/driveapimigration/DriveServiceHelper.java

id 来自查询结果,其中 id 是附加到从 Drive 查询返回的查询信息的一部分。id 是要检索的文件的文件 id。将它传递给 readFile ,它会给你文件内容,你可以在本地保存到 java.io.File 例如 fileOutputStream.write(contents.getBytes()); 其中的内容是pair.second。然后你就会有希望(因为有时我们有更多的工作要做)完全相同的 java.io.File。

如果您在链接示例中也需要一个基本查询,您可以看到一个基本查询,但它缺少一些重要信息,因为根据您的操作,您可能需要检查是否已删除、获取文件大小、修改时间、md5、设置顺序等. 您可能需要查看https://developers.google.com/drive/api/v2/reference/files/list和https://developers.google.com/drive/api/v3/reference/files等来计算那出来。如果文件足够多,也会有分页要求。

我知道从代码中使用 Drive 有点疯狂(好吧,无论如何它对我来说都是哈哈)所以坚持下去=)