360-320-6221 caperren@caperren.com

Part 4 – Extras

More on Scopes

Up to this point, I’ve show ways to get some simple data from the Google API engine. However, this doesn’t really give a good idea of what it can actually do. Well, as it turns out, the amount it is capable of doing is directly related to the scope you give to the project. All of my examples used the read-only scope, but all of the following are available.

'https://www.googleapis.com/auth/drive'
'https://www.googleapis.com/auth/drive.readonly'
'https://www.googleapis.com/auth/drive.appfolder'
'https://www.googleapis.com/auth/drive.apps.readonly'
'https://www.googleapis.com/auth/drive.file'
'https://www.googleapis.com/auth/drive.install'
'https://www.googleapis.com/auth/drive.metadata'
'https://www.googleapis.com/auth/drive.metadata.readonly'
'https://www.googleapis.com/auth/drive.photos.readonly'
'https://www.googleapis.com/auth/drive.scripts'

The top scope, simple ‘drive’ is the full read/write access to drive. So if your plan is to move files around, change contents, make comments, or upload new files, that’s the scope you’d want to use. However, because it’s powerful, Google recommends only using it when you absolutely have to. Other of the scopes listed are highly specific such as the install scope which give the ability to install apps into a user’s google drive workspace. Or, one such as the ‘drive.file’ scope can allow a user to provide access to a single file in a user’s drive and nothing else. A full breakdown of what the scopes give access to can be viewed HERE. It’s important to understand what scope is needed for the project you’re working on before you start so you don’t end up with a slew of permissions errors.

Testing Permissions and API Calls

In the previous examples, I referenced the Google APIs explorer but really didn’t delve into how useful it really is. Let’s say you want to see if a particular API call will have the necessary permissions under a certain scope. Or maybe it’s a pain to dig through your console log to find out what the contents of the response is going to be. Under other conditions, you might just have to test all of the scopes until you got them right or have no choice but to dig through the log. However, with the developer console, none of this is necessary.

Once you’ve initially set up credentials in the developer console, the API explorer will let you make OAuth authenticated test calls using their interface. I’m sure it goes without saying, but this is incredibly powerful. For example, when I was learning how the changes module worked, I used the ‘drive.changes.getStartPageToken’ page on the developer console to get an initial token, then put that token into the ‘drive.changes.list’ page to get a JSON string formatted response with the data I would get from making that call from my application. This made it incredibly easy to figure out how to parse the response data.

Searching

I couldn’t finish this series off without mentioning how to search for something in a Google Drive account. While I didn’t need it for anything I had examples for, I know this will be a common question from a lot of people. The answer is to use the ‘drive.files.list’ call with an optional parameter ‘q’ that is a search query. A listing of all supported search parameters can be found HERE. Here is an example of a call.

var request = gapi.client.drive.files.list({
    'pageSize': 10,
    'fields': "nextPageToken, files(id, name)",
    'q': "name='myFileName'"
});

 

Final Thoughts

While my examples only showed read related activities, the API is capable of everything needed to manipulate and change all the data in Google Drive that you could want.This was a fun little intro into developing for a Google product, something which I’ve never done before, and look forward to continuing in the future. Have fun developing!