Python Applications with Google Drive SDK

Paul Bailey
http://bit.ly/pydrive

Example Google Drive Apps

  • image editor: pixlr
  • online diagramming: draw.io
  • code editor: neutron drive (the presenter's app)

Getting Started

  • install App Engine SDK
  • install Google Drive SDK
  • Go to the API console, turn on Drive API and Drive SDK
  • Setup API Access to create an OAuth ID

In [1]:
# in local_settings.py...

PRIV_GOOGLE_API_CLIENT_ID = '...'
PRIV_GOOGLE_API_CLIENT_SECRET = '...'

OAuth

Your application directs the user to the 3rd party, where they log in. That service (Google) then sends you a token that you can use to make requests to the 3rd party on the user's behalf.


In [ ]:
from google.appengine.ext import db
from oauth2client.appengine import CredentialsProperty

class Credentials(db.Model):
    credentials = CredentialsProperty()
    created = db.DateTimeProperty(auto_now_add=True)

# in settings.py...

# you generally try to use the least permissive scopes you need
# don't ask for the full drive unless you absolutely need to
ALL_SCOPES = (
    '...',
    '...',
)

In your views, you'd have a view to redirect the user to the Google login page if the don't have a cookie with the OAuth token.

Communicating with Google Drive

Like with Amazon S3, all resources are identified with IDs, rather than actual paths (even though the IDs might look like paths).
Don't need to write your own file picker or sharing dialog
There is also the Realtime API, which lets people edit a file at the same time

Other

Check out PyDrive; shows a lot of common tasks
Recent changes make a lot of things simpler
More complex example: DrEdit