Monday, June 18, 2012

OAuth 2.0 Playground and Google Drive API

Author Photo
By Nicolas Garnier, Developer Relations

Last November, we launched the OAuth 2.0 Playground, a tool enabling you to easily experiment with the OAuth 2.0 protocol and APIs that use the protocol. We recently added a convenient way for you to test the newly launched Google Drive API by allowing you to open Drive files with the OAuth 2.0 Playground along with extending the capabilities of the Playground to support file upload and multipart requests.

The Drive API uses a per-file security model. When using the https://www.googleapis.com/auth/drive.file scope you will only be able to access the files that your app has created or that were opened through the Google Drive interface with your app. This made it impossible to read any existing Drive files using the OAuth 2.0 Playground by simply authorizing the Drive scope. To make this possible we are providing a way to open files from the Drive UI with the OAuth 2.0 Playground. All you need to do is add the OAuth 2.0 Playground application to Google Drive.

OAuth 2.0 Playground as a Drive app on the Chrome Web Store

To allow you to experiment with the Drive-initiated OAuth 2.0 flow and open existing Drive files using the API we've made the OAuth 2.0 Playground installable as a Drive app from the Chrome Web Store.




OAuth 2.0 Playground listing on the Chrome Web Store

After installing the OAuth 2.0 Playground you will be able to open files directly from Drive using the OAuth 2.0 Playground by simply right clicking on a file in Drive > Open with > OAuth 2.0 Playground.




Right click on a file in Drive > Open with > OAuth 2.0 Playground

This makes sure the Playground is granted access to the file and then redirects the user to the Playground after initiating an OAuth flow. The Playground will have the auth code and the file metadata URL pre-filled so you'll then be able to conveniently finish the OAuth flow and fetch the file's metadata.


{
"kind": "drive#file",
"id": "0B9-4449Da_xrQlIzdG1TcUJDaTQ",
"etag": "\"apVthTUIHAT9tjL_sIEpc--sy_s/MTMzOTY4MDk…
"title": "drive_logo.png",
"mimeType": "image/png",
"labels": {
 "starred": false,
 "hidden": false,
 "trashed": false
},
"createdDate": "2012-06-14T13:36:29.054Z",
"modifiedDate": "2012-06-14T13:36:29.054Z",
"modifiedByMeDate": "2012-06-14T13:36:28.905Z",
"lastViewedDate": "2012-06-14T15:53:18.150Z",
"parentsCollection": [
 {
  "id": "0AN-4449Da_xrUk9PVA",
 }
],

Drive file’s metadata fetched by the OAuth 2.0 Playground

Specify file content as request body

Along with manually specifying text, you can now choose to send file content as part of the request body with the Playground. To do that, simply select a file in the Request Body.



In the case where you choose to specify both some manually entered text and a file as the request body, we will automatically convert your request to a multipart request as shown below.


POST /upload/drive/v1/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Content-length: 1759
Content-type: multipart/related; boundary="===============736

Authorization: OAuth ya29.1234567890
--===============7362915341248842466==
Content-type: application/json


{
"title": "drive_logo.png",
"mimeType": "image/png"
}
--===============7362915341248842466==
Content-type: image/png

<BINARY DATA - 1526 Bytes>
--===============7362915341248842466==--




A multipart request sent from the OAuth 2.0 Playground

This allows you, for instance, to create new files on Drive with one single request to the Drive API file upload endpoint by sending both the metadata of the file and the file’s content.

We hope the OAuth 2.0 Playground Drive application and the new file upload features will help you during the development of Drive-enabled web applications. If you have any questions regarding the Drive API don’t hesitate to ask us on Stack Overflow and if you’d like to reach out to the OAuth 2.0 Playground team please do so on our Google Group.


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.

Posted by Scott Knaster, Editor

No comments:

Post a Comment