一个应用程序在 Play 商店上发布,它正在使用“应用程序数据文件夹”进行备份恢复,使用 Drive API .一切正常。然而,根据谷歌的公告,该 API 将于 2019 年 12 月 6 日关闭。因此,为了支持现有用户,我一直在根据 migration guidlines 迁移到最新的 API。和 an official sample app .
我可以使用下面的代码 ( from the official link ) 成功进行身份验证。
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);
我还使用了正确的范围 - 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();
}
});
}
但是,每当我尝试使用下面的代码 (from the official link) 访问 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());
}
它抛出以下错误
{
"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."
}
请帮助我修复错误。我相信,由于已发布的版本一切正常,因此在 Google API 控制台上进行配置时,一切都应该是正确的。
最佳答案
我可能找不到所有有此问题的线程,但我可以尝试提供一些帮助。
主要答案
如果您和我一样使用 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>; }
次要答案
请注意,您不要需要通过 Android 的 Drive rest API 使用 key / token ,就像您可能会从其他解决方案中找到的那样(这可能也不会造成伤害,但会造成伤害)。它在这里与人们在其他地方谈论的内容不匹配(在这里他们不知道他们在谈论什么)。
请在此处查看我的笔记以获取更多信息:Google Drive via OAuth release version receives dailyLimitExceededUnreg
如果您在调试构建中遇到问题,那么您没有正确执行所有操作。我的笔记应该能让您走上正轨。
如果您需要进一步的帮助,我可能会提供帮助,因为这太疯狂了。
使用新 API 从驱动器获取文件的示例
从下面的链接可以看到
public Task<Pair<String, String>> readFile(String fileId)
id 来自查询结果,其中 id 是附加到从云端硬盘查询返回的查询信息的一部分。 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 有点疯狂(好吧,反正对我来说是这样哈哈)所以坚持住 =)
关于android - 在 Android 中迁移到最新的 Google Drive API 时出错 - 超出了未经身份验证使用的每日限制。继续使用需要注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354476/