Google云端硬盘无需证书即可下载公共文件

时间:2020-04-14 22:19:26

标签: java download google-drive-api

我正在尝试下载公共的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下载文件而无需任何凭据?

1 个答案:

答案 0 :(得分:0)

注意事项:

使用代码至少需要使用API​​密钥。 如果未在控制台中激活API密钥,通常也会显示您的错误。

解决方案:

要从没有任何API密钥或OAuth2凭据的Google驱动器中下载文件,您可以采用以下两种策略:

小文件:

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

// 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