Pages

Wednesday, March 11, 2015

Exporting native Google documents with the Google Drive SDK

Three months ago, we launched Google Drive along with the Google Drive SDK. The SDK allows applications to manage files that users have uploaded to Drive and to integrate deeply in the Google Drive UI. Today, we’ve just extended the SDK to allow developers to interact with native Google Docs types such as Google Spreadsheets, Presentations, Documents, and Drawings.

We now provide an easy way to export native Google documents through the Google Drive API. We also allow native Google documents to be opened directly from within the Google Drive UI using third-party applications.

If your application is already a Drive-installable app, you can enable this feature by checking the Allow users to open files that can be converted to a format that this app can open option in the Google APIs Console under Drive SDK > Import:

Enabling the Import feature of your Drive application

When this feature is enabled, your application will show up under the “Open with” menu in the Google Drive Web UI for the file formats you support. Here’s how it works: if your application supports opening one of the possible export formats for the Google document, users will be able to open that Google document with your application. So for instance, if your application is configured to open PDF files, then because Google Documents are exportable to PDF, users will be able to use your application to open those documents as shown below.

Right clicking on a Google Document > Open with

When opening a native Google Document with your application from the Google Drive UI, we will pass the following JSON Object in the state URL parameter of the OAuth 2.0 redirect request that is forwarding the user to your website.


 
{
"exportIds": ["file_id"],
"action":"open"
}
 

Then you can use the file ID contained in the JSON object to query the Google Drive API and fetch the file’s metadata. Note that the state URL parameter is different when opening regular files as we use the JSON attribute exportIds instead of ids.

Unlike the metadata of regular files which contain a downloadUrl attribute which you can use to download the file’s content, the metadata for native Google documents contains a collection of export URLs. These URLs - one for each supported export format - are listed under the attribute exportLinks, as shown in the HTTP request/response below.

Request:


 
GET /drive/v2/files/<file_id> HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <access_token>
 

Response:


 
HTTP/1.1 200 OK
Status: 200
...

{
"kind": "drive#file",
"id": "<file_id>",
...
"exportLinks": {
"application/vnd.oasis.opendocument.text": "https://docs.google.com/...",
"application/msword": "https://docs.google.com/...",
"text/html": "https://docs.google.com/...",
"application/rtf": "https://docs.google.com/...",
"text/plain": "https://docs.google.com/...",
"application/pdf": "https://docs.google.com/..."
},
...
}
 

You can query any of these export URLs using an authorized request to download the Google document in your prefered export format.

Below is the full list of supported export formats -- and their associated MIME types -- for the different types of native Google documents:

Google Documents:

  • Text File (TXT) - text/plain
  • Portable Document Format (PDF) - application/pdf
  • OpenDocument Text Document (ODT) - application/vnd.oasis.opendocument.text
  • Microsoft Word (DOC) - application/msword
  • Hypertext Markup Language (HTML) - text/html
  • Rich Text Format File (RTF) - application/rtf

Google Spreadsheets:

  • Portable Document Format (PDF) - application/pdf
  • Microsoft Excel Spreadsheet (XLS) - application/vnd.ms-excel
  • OpenDocument Spreadsheet (ODS) - application/x-vnd.oasis.opendocument.spreadsheet

Google Presentations:

  • Text File (TXT) - text/plain
  • Portable Document Format (PDF) - application/pdf
  • Microsoft Office Open XML Format Presentation (PPTX) - application/vnd.openxmlformats-officedocument.presentationml.presentation

Google Drawings:

  • Scalable Vector Graphics (SVG) - image/svg+xml
  • JPEG Images (JPEG) - image/jpeg
  • Portable Network Graphics (PNG) - image/png
  • Portable Document Format (PDF) - application/pdf

Please check out the Google Drive SDK documentation if you’d like to learn more, and feel free to ask any questions you may have on Stack Overflow.


Nicolas Garnier profile | twitter | events

Nicolas Garnier joined Google’s Developer Relations in 2008 and lives in Zurich. He is a Developer Advocate focusing on Google Apps and Web APIs. Before joining Google, Nicolas worked at Airbus and at the French Space Agency where he built web applications for scientific researchers.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.