5 things you didn't know you could do with the Google Drive API

Wednesday, August 1, 2012 | 2:53 PM

Labels:

Have you tried using the Google Drive API? If so, you’re aware that it allows you to programmatically manage a user’s Google Drive and build applications to manipulate files stored in the user’s account. However, the API might still be capable of doing a few things you didn’t know about. Here is a list of five specific use cases and how each of them can be addressed with the API.

1) Sharing a file with the world

When a file in Google Drive is shared publicly, it can be downloaded without authentication at the URL provided by the API in the webContentLink field of the Files resource. To retrieve that value, send a GET request to retrieve the file metadata and look for the webContentLink element in the JSON response, as in the following example:

{
  "kind": "drive#file",
  "id": "0B8E...",
  "etag": "WtRjAP...",
  "selfLink": "https://www.googleapis.com/drive/v2/files/0B8E...",
  "webContentLink": "https://docs.google.com/a/google.com/uc?id=0B8E...",
  ...
}

2) Granting comment-only access to a user

When setting permissions for a file with the Drive API, you can choose one of owner, writer and reader as the value for the role parameter. The Drive UI also lists another role, commenter, which is not allowed for that parameter.

In order to grant comment-only access to a user with the Drive API, you have to set the role parameter to reader and include the value commenter in the list of additionalRoles, as in the following example:

{
  "kind": "drive#permission",
  ...
  "role": "reader",
  "additionalRoles": [
    "commenter"
  ],
  ...
}

3) Listing all files in the root folder

It is possible to restrict the list of files (and folders) returned by the Drive API by specifying some search criteria in the q query parameter. Each file has a parents collection listing all folders containing it, and the root folder in Google Drive can be conveniently addressed with the alias ‘root’. All you need to do to retrieve all files in that folder is add a search query for element with ‘root’ in their parents collection, as in the following example:

GET https://www.googleapis.com/drive/v2/files?q='root' in parents

Remember to URL-encode the search query for transmission unless you are using one of the available client libraries.

4) Finding how much quota is available in the user’s account

Your application might need to know if users have enough available quota to save a file, in order to handle the case when they don’t. Quota information is available in the About feed of the Drive API:

{
 "kind": "drive#about",
 ...
 "quotaBytesTotal": "59055800320",
 "quotaBytesUsed": "14547272",
 "quotaBytesUsedInTrash": "511494",
 ...
}

The feed includes three values related to quota management: quotaBytesTotal, quotaBytesUsed and quotaBytesUsedInTrash. The first value indicates the total amount of bytes available to the user (new accounts currently get 5GB for free) while the second one tells how many of those bytes are in use. In case you need to get more free space, you can use the last value to know how many bytes are used by files that have been trashed. An application might use this value to recommend emptying the trash bin before suggesting to get additional storage.

5) Discovering if one of the user’s apps can open a file

Google Drive allows users to store any kind of file and to install applications to open file types that are not directly supported by the native Google applications. In case you need to know what applications are installed and what file types each of them can open, you can retrieve the Apps feed and look for the primaryMimeTypes and secondaryMimeTypes elements for supported MIME types or primaryFileExtensions and secondaryFileExtensions for file extensions:

{
   "kind": "drive#app",
   "name": "Pixlr Editor",
   ...
   "primaryMimeTypes": [
    "image/psd",
    "image/pxd",
    "application/vnd.google-apps.drive-sdk.419782477519"
   ],
   "secondaryMimeTypes": [
    "image/png",
    "image/jpeg",
    "image/gif",
    "image/bmp"
   ],
   "primaryFileExtensions": [
    "pxd",
    "psd"
   ],
   "secondaryFileExtensions": [
    "jpg",
    "png",
    "jpeg",
    "bmp",
    "gif"
   ],
   …
}

Note: to access the Apps feed you have to request access to the https://www.googleapis.com/auth/drive.apps.readonly OAuth scope.

Claudio Cherubino   profile | twitter | blog

Claudio is an engineer in the Google Drive Developer Relations team. Prior to Google, he worked as software developer, technology evangelist, community manager, consultant, technical translator and has contributed to many open-source projects. His current interests include Google APIs, new technologies and coffee.

3 comments:

dyoung said...

Hi, thanks for the information, very useful. One question, does the Drive API support sharing a file with a list of users? I don't mean by invoking a google supplied library/script, but by programmatically using the Drive API. I see that the Permission in the API is somehow related to that, but i don't see where you specify the list of users to share. Thanks.

Jayapal Chandran said...

For me point no 3 is still listing all the files in the order of last modified thought i used q='root' (urlencoded).

Has google changed its rule now ( now being 11-mar-2013)

?

suleman mirza said...

Hi Claudio can you help me with this, I am uploading and updating files and creating folders to Google drive, but when I am sending the request, the title doesn't change its just UNTITLED, my request example is,

url: 'https://www.googleapis.com/upload/drive/v2/files?uploadType=media',
type: "post",
headers: {
"Authorization": "Bearer " + accessToken,

},
data: {
"mimeType": "application/vnd.google-apps.folder",
"title": "sampleFolder2"
}
}).then(function (success) {
var response = JSON.parse(success.response);
var x = 2;
}, function (error) {
var x = 4;
});