0

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?

Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • 1
    In your script, the export method of Drive API is used. In this case, an API key is required to be used even when it's for downloading the publicly shared file. If you want to download such file without using the API key and access token, there are the following 2 patterns. From your script, it supposes that you try to export the publicly shared Google Docs. – Tanaike Apr 14 '20 at 23:44
  • 1
    Pattern 1: When the file size is small, you can download it using `exportLinks` retrieved by the get method of Drive API. Pattern 2: When the file size is large, it is required to do 2 processes. You can see the sample flow at https://stackoverflow.com/a/48133859/7108653 In this case, I think that it is required to directly request to the endpoint without googleapis. If this was not the direction you expect, I apologize. – Tanaike Apr 14 '20 at 23:44

1 Answers1

0

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

Alessandro
  • 2,848
  • 1
  • 8
  • 16
  • Thanks a lot for your answer. I tried with a straight get and the same 403. driveService.files().get(fileId).execute() I think there is a get I don't see, can you point me to the right method? – Liviu Stirb Apr 15 '20 at 19:44
  • Sorry, I think I haven't been clear in my answer. I have updated it. You will have to fetch the `exportLinks` obtained through the API. You cannot use the API without any credentials. I am assuming you already have the public export link. You might as well get the links with your API key and then use them in your code without authorization. – Alessandro Apr 16 '20 at 09:01
  • Hello @Alessandro, Do we need API keys to get the `exportLinks` programmatically in C#? – JackFrost Sep 15 '22 at 13:27
  • It will be helpful if you can show us how to get `exportLinks` using driver `get()`. Thank you. I am trying to achieve downloading files from Google drive with out any credentials. – JackFrost Sep 15 '22 at 13:30