Google drive download public file without credentials

て烟熏妆下的殇ゞ 提交于 2021-02-11 17:01:14

问题


I'm trying to download a public google drive file without using any credentials. My code looks like:

    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);

But this will get a 403 from google:

{
  "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."
}

Is it possible to programmatically download a file from google without having any credentials?


回答1:


Considerations:

Using your code will require at least the use of an API key. Your error usually shows up as well when the API key is not activated in your console.

Solution:

To download files from Google drive without any API key nor OAuth2 credentials, you can follow 2 strategies:

Small Files:

Using one of the exportLinks obtained with the Drive API .get() method.

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

Bigger Files:

Follow the steps in this Stack Overflow post: https://stackoverflow.com/a/48133859/7108653

References:

Drive API Files



来源:https://stackoverflow.com/questions/61218203/google-drive-download-public-file-without-credentials

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!