Introduction
In a blog post last week, we introduced readers to performing uploads and downloads files to/from Google Drive from a simple Python command-line script. In an official Google blog post later that same day, the Google Drive API team announced a new version of the API. Great timing huh? Well, good thing I knew it was coming, so that I could prepare this post for you, which is a primer on how to migrate from the current version of the API (v2) to the new one (v3).As stated by the Drive team, v2 isn't being deprecated, and there are no new features in v3, thus migration isn't required. The new version is mainly for new apps/integrations as well as developers with v2 apps who wish to take advantage of the improvements. This post is intended for those in the latter group, covering porting existing apps to v3. Ready? Let's go straight to the action.
Migrating from Google Drive API v2 to v3
Most of this post will be just examining all the "diffs" between the v2 code sample from the previous post (renamed fromdrive_updown.py
to drive_updown2.py
) and its v3 equivalent (drive_updown3.py
). We'll take things step-by-step to provide more details, but let's start with all the diffs first:--- drive_updown2.py 2018-03-11 21:42:33.000000000 -0700
+++ drive_updown3.py 2018-03-11 21:44:57.000000000 -0700
@@ -11,23 +11,24 @@
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
-DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
+DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
FILES = (
- ('hello.txt', False),
- ('hello.txt', True),
+ ('hello.txt', None),
+ ('hello.txt', 'application/vnd.google-apps.document'),
)
-for filename, convert in FILES:
- metadata = {'title': filename}
- res = DRIVE.files().insert(convert=convert, body=metadata,
- media_body=filename, fields='mimeType,exportLinks').execute()
+for filename, mimeType in FILES:
+ metadata = {'name': filename}
+ if mimeType:
+ metadata['mimeType'] = mimeType
+ res = DRIVE.files().create(body=metadata, media_body=filename).execute()
if res:
print('Uploaded "%s" (%s)' % (filename, res['mimeType']))
if res:
MIMETYPE = 'application/pdf'
- res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
+ data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
if data:
fn = '%s.pdf' % os.path.splitext(filename)[0]
with open(fn, 'wb') as fh:
We'll start with the building of the service endpoint, with the trivial change of the API version string from 'v2'
to 'v3'
:-DRIVE = build('drive', 'v2', http=creds.authorize(Http())) +DRIVE = build('drive', 'v3', http=creds.authorize(Http()))The next change is the deprecation of the conversion flag. The problem with a Boolean variable is that it limits the possible types of file formats supported. By changing it to a file mimeType instead, the horizons are broadened:
FILES = ( - ('hello.txt', False), - ('hello.txt', True), + ('hello.txt', None), + ('hello.txt', 'application/vnd.google-apps.document'), )Your next question will be: "What are the mimeTypes for the supported Google Apps document formats?" The answers can be found at this page in the official docs. This changes the datatype in our array of 2-tuples, so we need to change the loop variable to reflect this... we'll use the mimeType instead of a conversion flag:
-for filename, convert in FILES: +for filename, mimeType in FILES:Another change related to deprecating the
convert
flag is that the mimeType isn't a parameter to the API call. Instead, it's another piece of metadata, so we need to add mimeType
to the metadata object.Related to this is a name change: since a file's name is its name and not its title, it makes more sense to use "
name
" as the metadata value:- metadata = {'title': filename} + metadata = {'name': filename} + if mimeType: + metadata['mimeType'] = mimeTypeWhy the
if
statement? Not only did v3 see a change to using mimeTypes, but rather than being a parameter like the conversion flag in v2, the mimeType has been moved into the file's metadata, so if we're doing any conversion, we need to add it to our metadata
field (then remove the convert
parameter down below).Next is yet another name change: when creating files on Google Drive, "
create()
" makes more sense as a method name than "insert()
". Reducing the size of payload is another key ingredient of v3. We mentioned in the previous post that insert()
returns more than 30 fields in the response payload unless you use the fields
parameter to specify exactly which you wish returned. In v3, the default response payload only returns four fields, including all the ones we need in this script, so use of the fields
parameter isn't required any more:- res = DRIVE.files().insert(convert=convert, body=metadata,
- media_body=filename, fields='mimeType,exportLinks').execute()
+ res = DRIVE.files().create(body=metadata, media_body=filename).execute()
The final improvement we can demonstrate: users no longer have to make an authorized HTTP GET request with a link to export and download a file in an alternate format like PDF®. Instead, it's now a "normal" API call (to the new "export()
" method) with the mimeType as a parameter. The only other parameter you need is the file ID, which comes back as part of the (default) response payload when the create()
call was made:- res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
+ data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
That's it! If you run the script, grant the script access to your Google Drive (via the OAuth2 prompt that pops up in the browser), and then you should get output that looks like this:
$ python drive_updown3.py # or python3 Uploaded "hello.txt" (text/plain) Uploaded "hello.txt" (application/vnd.google-apps.document) Downloaded "hello.pdf" (application/pdf)
Conclusion
The entire v2 script (drive_updown2.py) was spelled out in full in the previous post, and it hasn't changed since then. Below is the v3 script (drive_updown3.py) for your convenience which runs on both Python 2 and Python 3 (unmodified!):#!/usr/bin/env python
from __future__ import print_function
import os
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
FILES = (
('hello.txt', None),
('hello.txt', 'application/vnd.google-apps.document'),
)
for filename, mimeType in FILES:
metadata = {'name': filename}
if mimeType:
metadata['mimeType'] = mimeType
res = DRIVE.files().create(body=metadata, media_body=filename).execute()
if res:
print('Uploaded "%s" (%s)' % (filename, res['mimeType']))
if res:
MIMETYPE = 'application/pdf'
data = DRIVE.files().export(fileId=res['id'], mimeType=MIMETYPE).execute()
if data:
fn = '%s.pdf' % os.path.splitext(filename)[0]
with open(fn, 'wb') as fh:
fh.write(data)
print('Downloaded "%s" (%s)' % (fn, MIMETYPE))
)
Just as in the previous post(s), you can now customize this code for your own needs, for a mobile frontend, sysadmin script, or a server-side backend, perhaps accessing other Google APIs. Hope we accomplished our goal by pointing out some of the shortcomings that are in v2 and how they were improved in v3! All of the content in this and the previous post are spelled out visually in this video that I created for you.