AppStudio 6: Accessing Google Drive

searchGoogle Drive is a convenient way to store documents in the cloud and access them on any of your devices. It’s also an easy way to share documents. Millions of people use Google Drive – recent statistics show Google Docs to be as much as 5 times more popular than Microsoft Word.

The great news is that you can access the data in Google Drive from your AppStudio app. For example, you can read the data in a Google Sheet, modify it, and write it out again. This effectively turns the spreadsheet into a database for your app.

Volt makes this possible for everyone to use. To use Google Drive, your app needs to be deployed to a server. For security purposes, you have to give Google Drive the name of the server before it will authorize it to access your files. You can set up your own server for this or simply use Volt. After you deploy your app to Volt, enter the path which Volt gives you (something like “https://googlespreadsheet-signs-smoothly.volt.live”) and update the credentials.

Credentials
The trickiest part in the whole process is setting up the credentials. Follow the instructions in the Google Drive API article to do this. The result will be a Client_ID which needs to be added to your app.

Some sample code

Set up the credentials:

GoogleDrive.CLIENT_ID = "988...apps.googleusercontent.com"

Once the credentials are set up, your app can log into Google Drive with the following call:

GoogleDrive.authorize(authCallback)

If successful, authCallback is called with the value true, otherwise false.

Now that you’re signed on, you can do a number of things. Here’s how to ask for a list of files:

GoogleDrive.listFiles(listFilesCallback)

Files are identified by their FILE_ID. Here’s how to read in a spreadsheet:

GoogleDrive.getSpreadsheet(FILE_ID, getSpreadsheetCallback)

The results are returned in a 2 dimensional array. You can modify the contents of the array – here’s how to write it back out:

GoogleDrive.saveSpreadsheet(spreadsheet, FILE_ID, saveSpreadsheetCallback)

Offline Operation

Since Google Drive copies the files onto your system, you can access the files even if you’re offline. The version of the file will be whatever was last downloaded.

If you update a file while in offline mode, it will work. Next time there is a good connection, the file will be uploaded to Google Drive in the cloud. Be careful if there are multiple people using the document.

Sample

There’s a sample of this in the AppStudio Samples. It’s in in folder 7, Web Services. It’s called GoogleSpreadsheet.