Google 驱动器下载 public 没有凭据的文件

Google drive download public file without credentials

我试图在不使用任何凭据的情况下下载 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 密钥。 当 API 键未在您的控制台中激活时,您的错误通常也会出现。

解决方案:

要在没有任何 API 密钥或 OAuth2 凭据的情况下从 Google 驱动器下载文件,您可以遵循 2 种策略:

小文件:

使用通过 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 post 中的步骤进行操作:

参考文献:

Drive API Files