Download a file from Google Drive using Python

Reading time ~2 minutes

This Python script downloads a Google Sheets document from Google Drive as an Excel file.

The code is mostly from the official Google API site; the changes I made are that Credential path is set to the directory where the script is excuted and that Scope is set from metadata.readonly to readonly for reading the content of files we want to download. (For more details on Scope, you can refer to the official document here)

Tested environment: Ubuntu 16.04, Python 3.5.2

Usage

To get a credential for this script, finish Step 1 following instructions here

Change the value of FILE_ID at line 27 to that of the actual file you want to download.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/env python3

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import io
from apiclient.http import MediaIoBaseDownload

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

CREDENTIAL_DIR = './credentials'
CREDENTIAL_FILENAME = 'drive-python-quickstart.json'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Drive File Export Example'
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'

FILE_ID = 'your file id'
EXCEL = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
EXPORTED_FILE_NAME = 'exported_file.xlsx'

def get_credentials():
    credential_dir = CREDENTIAL_DIR
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir, CREDENTIAL_FILENAME)

    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    request = service.files().export_media(fileId=FILE_ID, mimeType=EXCEL)
    fh = io.FileIO(EXPORTED_FILE_NAME, 'wb')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print('Download %d%%.' % int(status.progress() * 100))


if __name__ == '__main__':
    main()

If you run this script on a remote server via terminal, you should add --noauth_local_webserver option.

$ ./export_example.py --noauth_local_webserver

The script attempts to open an authorization URL to access to your Google Drive if the script has failed to find a valid credential. If you are running the script on a remote server, Lynx, a text web browser is executed to open the URL but Google Drive doesn’t accept a request from text browsers. This option provides a URL to proceed an authorization for you instead opening a text browser on the server, so you can just open your web browser and enter the URL.

After granting the permission, an authorization code will appear. Copy the code and paste it to the terminal.

Finished authorization, it starts downloading.

kiwi campus

가족여행차 샌 프란시스코에 갔다가 오랜만에 버클리 교정을 구경하러 갔다.돌아다니다보니 Sather gate 근처의 분수대 주변에 이런 게 돌아다니고 있었다.처음엔 학생이 만든 장난감 정도로 생각했다. 폰에 외장(?)렌즈를 달아서 꽂아놨는데 이게 ...… Continue reading