Automated Gsuite Export

How to download documents like Google Docs and Sheets programmatically

Published: Friday, Jan 22, 2021 Last modified: Monday, Apr 8, 2024

Update: Rclone is the best DX CLI to get retrieve content from G Suite/Workspaces

Automated authentication in order to use Google Drive API is non-trivial.

Now there are two categories of credentials for accessing APIs:

  1. Service account aka credentials.json (non-interactive) but can’t really impersonate you
  2. Oauth with ClientID et al. (interactive) but can impersonate you

The “API key” has very limited scope, only for checking quota and access, not actually useful for APIs, IIUC.

Service account

Service accounts have an issue whereby it needs domain-wide-delegation by your G Suite Administrators (no one knows who these people are in large companies) to impersonate yourself with your own company email.

Note: Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren’t subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of G Suite end users to share documents outside of the domain would not apply to service accounts.


The work around is to use your credential’s email:

$ grep email credentials.json
  "client_email": "$name@$",

And explicitly share that email address with documents you want to export with this account.

from googleapiclient.discovery import build
from google.oauth2 import service_account
SCOPES = ['']
SERVICE_ACCOUNT_FILE = 'credentials.json'

# We use the SERVICE_ACCOUNT_FILE we just downloaded and the SCOPES we defined to create a Credentials object.
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# Remember, you must have created credentials.json with domain-wide delegation!
credentials = credentials.with_subject('')

# We build a drive_v3 service using the credentials we just created
service = build('drive', 'v3', credentials=credentials)

files = service.files()
fconr = files.export(fileId='141g8UkQfdMQSTfIn475gHj1ezZVV16f5ONDxpWrrvts',

fcont = fconr.execute()


file = open("/tmp/sample.doc", "wb")

Via this notebook and this answer by Jayson Salazar

Oauth token interchange from ClientID

After setting the API scope, generating/maintaining the token from Authorization code seems non-trivial to me. The Oauth Playground (with its own ClientID) does this automatically for you. But to do this yourself from Python appears non-trivial to me.

When you use the “Try this API” aka “google-apis-explorer” Google application, again the complex Oauth interchange refresh dance is already done for you.

[ "access_token", "expiry", "refresh_token", "token_type" ]

However if you want your own App from the CLI to do this, it appears non-trivial as it needs some explicit Oauth consent interchange However once you do have the Bearer token aka access_token, it’s straightforward to work with unlike credentials.json’s [ "auth_provider_x509_cert_url", "auth_uri", "client_email", "client_id", "client_x509_cert_url", "private_key", "private_key_id", "project_id", "token_uri", "type" ].

curl -H "Authorization: Bearer $token" -o doc.pdf \${id}/export?mimeType=$mimeType

Here I believe you impersonate yourself via the ClientID, without the scary Share outside of organization process needed for the aforementioned $name@$ service accounts.