Download Files Without Authorization From Google Drive

Overview

In this article, files can be downloaded without authorization.

Description

When we download files from Google Drive, it usually has to use Drive API. In order to use Drive API, access token is required. If you want to make your friends download files from your Google Drive, the authorization process is to take time. Also Web Link for each files can be used. But it has to set for each files. So I proposal this.

As a sample, I introduce a script for downloading the files using Web Apps. In this sample, it changes a file to a byte slice and send it as text data. Then, it reconstructs it. Of course, base64 encode can be used for this. But the data size for using base64 is much larger than that for using the byte slice.

At this method, the project files including GAS script cannot be downloaded. When Google Docs are downloaded using this method, those are downloaded as PDF file. It may be the specification of Google.

By the way, also you can upload files without authorization to Google Drive using this method.

Usage

1. Deploy Web Apps

  1. Open the Script Editor.
  2. On the Script Editor
    • File -> Manage Versions -> Save New Version
    • Publish -> Deploy as Web App
    • At Execute the app as, select “your account”
    • At Who has access to the app, select “Anyone, even anonymous”
    • Click “Deploy”
    • Copy “Current web app URL”
    • Click “OK”

2. Paste following script on Script Editor.

function doPost(e) {
  return (function(id){
    var file = DriveApp.getFileById(id);
    return ContentService
          .createTextOutput(JSON.stringify({
            result: file.getBlob().getBytes(),
            name: file.getName(),
            mimeType: file.getBlob().getContentType()
          }))
          .setMimeType(ContentService.MimeType.JSON);
  })(e.parameters.id);
}

3. At local pc, use following script.

import numpy as np
import requests
r = requests.post(
    "https://script.google.com/macros/s/### Your ID ###/exec",
    data={"id": "### File ID ###"}
)
f = open(r.json()["name"], "bw")
f.write(np.array(r.json()["result"], dtype=np.uint8))
f.close()
print("Filename = {0}, MimeType = {1}".format(r.json()["name"], r.json()["mimeType"]))

Files output from this sample have no extension. So please add the extension for each mimeType to the files.

Appendix

If you want to download with authorization, there are 3 patterns.

  1. Download script and project including script.
  2. Download Google Docs (spreadsheet, document, presentation and so on). In this case, Google calls “Export”.
  3. Download files except for Google Docs.

 Share!