How to use Google Drive API from Google App Engine?
Although Google App Engine and Google Drive are both Google products, unfortunately they are not directly linked. Google Drive APIs can be accessed by the google-api-python-client
library, which you have to install.
The process can be found at Python Google Drive API Quickstart Guide, and the summarized form is as follows:
On Google's side: Allow Drive API Access for your GAE program
- Activate Drive API. Click the Go to credentials button to continue...
- Create your consent screen: Setup your OAuth Consent Screen as Google will throw weird errors if this has not been set up:
- Click on the OAuth Consent Screen tab
- Select an Email address and enter a Product name.
- Get credentials:
- Click on the Credentials tab
- Select Add credentials and then OAuth 2.0 client ID. Choose your application type, and enter the relevant details. You can change them later!
- Back on the Credentials tab, download the JSON credentials (all the way to the right in the table, the download button only appears when you hover near it). Rename it
client_secret.json
and place it in your root code directory. You will need this to request credentials from users.
On your side: Download the
google-api-python-client
library, unzip it in your code directory and runpython setup.py install
. This will install the library which holds many Google product's APIs.Now you are ready to use the Drive API. You can test your access using the sample code. Read it because it's a good guide for writing your own code! If you are accessing user data, you will need to request user credentials when they log in and most probably store them. Then, to use the API, the easiest way would be to get the
service
object:import httplib2 from apiclient import discovery credentials = get_credentials() #Your function to request / access stored credentials #Authorise access to Drive using the user's credentials http = credentials.authorise(httplib2.Http()) #The service object is the gateway to your API functions service = discovery.build('drive', 'v2', http=http) #Run your requests using the service object. e.g. list first 10 files: results = service.files().list(maxResults=10).execute() # ... etc ... Do something with results
Above code snippet is modified from sample code.
The Reference API for Google Drive can be found here.
The same general procedure is required to link GAE to other Google product's APIs as well e.g. Calendar. All the best writing your program!