Let web app users save and open files in Google Drive using API v3 -- authorize, upload, get, update, and use the Google file picker.
I wanted to let users of my web app save and open files in Google Drive, but it took several days of research to get all the details right.
Google documentation for Drive API v3 is light on examples, and doesn’t cover the JavaScript gapi
interface.
Internet search results for using the Drive API with JavaScript are often v2-specific or are for nodejs, which works differently than JavaScript in a browser.
And testing on your local machine requires some unexpected setup.
This is the demo I wish I had.
- Use this wizard to create a project in the Google Developers Console. Notes:
- Specify "User Data" (not "Application Data")
- Specify scope
https://www.googleapis.com/auth/drive.file
- Specify application type “Web Application”
- For “Authorized JavaScript origins” add
http://lvh.me
- On the credentials page, click
+ Add Credentials
and choose "API Key" - On the OAuth consent screen page, click
+ Add Users
and enter your Google account email address - On the Enable APIs page, search and enable the “Google Picker API”
- Verify that the dashboard table includes “Google Drive API” and “Google Picker API”
- In
index.html
, replaceapiKey
,clientId
, andprojectNumber
as directed in the code comments
- In a shell, cd to the repo folder and start a web server, but use port 80 instead of 8000. The Google APIs won’t work on other ports.
- Point your browser to http://lvh.me/index.html (
lvh.me
resolves to127.0.0.1
, akalocalhost
, but the Google API calls won't work if you use those directly) - Click the buttons in order:
Authorize
connects to your Google Drive- When it says "Google hasn’t verified this app", click
Continue
, check the box for "See, edit, create, and delete only the specific Google Drive files you use with this app", and clickContinue
- When it says "Google hasn’t verified this app", click
Upload
creates a file calledtest.json
with mime typeapplication/my.app
containing{"hello":"world"}
Get
retrieves the contents of that fileUpdate
updates the file to contain{"hello":"universe"}
Pick
opens the Google Drive Picker - select the file you just uploaded
- Watch the console messages. You should see:
Authorized. Token: ya29.a0ARrdaM9AcSmAJcmlwyfnTlZ1KGNWhSkSQqaSF7UJwI_nsGqZft7V5fqyNN39jdT5luQZL0evbQtwy-5etF6FA0isaIF_7yN91UX77GHGhonvgJOYzWWbpL3PHbsRs73v3GxdG8yrG9WrTMQ726MORtHJ_P9g3Q
Uploaded. Result:
{
"kind": "drive#file",
"id": "1XJUM6NVh94LpM6ILAhXGu6zYz1IFFOSJ",
"name": "test.json",
"mimeType": "application/my.app"
}
Fetched. Result: {"hello":"world"}
Updated. Result:
{
"kind": "drive#file",
"id": "1XJUM6NVh94LpM6ILAhXGu6zYz1IFFOSJ",
"name": "test.json",
"mimeType": "application/my.app"
}
Picked. FileId: 1XJUM6NVh94LpM6ILAhXGu6zYz1IFFOSJ
Fetched. Result: {"hello":"universe"}
Some posts suggest using a mime type of application/vnd.google-apps.drive-sdk
. For me trying to fetch files with that mime type failed with the error "Only files with binary content can be downloaded. Use Export with Docs Editors files."