java - 无需凭据即可使用 Google 云端硬盘下载公共(public)文件

标签 java download google-drive-api

我正在尝试在不使用任何凭据的情况下下载公共(public) Google 云端硬盘文件。我的代码如下所示:

    String fileId = "id_removed";
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Drive driveService = new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {

        }
    }).setApplicationName("test app").build();
    driveService.files().export(fileId, "txt")
            .executeAndDownloadTo(outputStream);

    String finalString = new String(outputStream.toByteArray());

    System.out.println(finalString);

但这会从 google 得到 403:

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

是否可以在没有任何凭据的情况下以编程方式从 Google 下载文件?

最佳答案

注意事项:

使用您的代码至少需要使用 API key 。 当您的控制台中未激活 API key 时,您的错误通常也会出现。

解决方案:

要在没有任何 API key 或 OAuth2 凭据的情况下从 Google 云端硬盘下载文件,您可以遵循 2 种策略:

小文件:

使用通过 Drive API .get() 方法获取的 exportLinks 之一。

// This won't require any authorization
InputStream in = new URL("https://docs.google.com/feeds/download/documents/export/Export?id=####&exportFormat=docx").openStream();
Files.copy(in, Paths.get("./the_doc.docx"), StandardCopyOption.REPLACE_EXISTING);

更大的文件:

按照此 Stack Overflow 帖子中的步骤操作:https://stackoverflow.com/a/48133859/7108653

引用文献:

Drive API Files

关于java - 无需凭据即可使用 Google 云端硬盘下载公共(public)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61218203/

相关文章:

java - 无法从 SQLite 数据库中删除行

python - 使用python下载时,我应该使用多线程还是多处理?

android - 使用暂停、继续和停止等基本任务控件管理多个文件下载

java - 如何解决这个 403 io 异常?

java.time : the simplest way to get a difference in hours between two LocalTime objects

java - 如何将我传递的值添加到我的方法中? java

java - 调试 Java JVM 崩溃 - native 代码访问

exception - Powershell下载文件无法正常工作

google-drive-api - Google云端硬盘API-更新电子表格数据

php - 从 Google Drive 中的 Txt、PDF、Doc 文件中提取电子邮件地址