Working with Google Drive REST API

Categories ASP.NET, PHP, Tutorials

In this article, we'll show you how to retrieve a list of files from Google Drive via REST API and display those files in your PHPRunner or ASPRunner.NET application. This is how it looks in the generated application. The list of files is retrieved from Google Drive and displayed in your own application. In this test app, we only implemented search and view functionality, but adding and deleting files can be done as well.


1. Create API credentials in Google Cloud console

Proceed to Google Cloud console. Under APIs & Services -> Credentials click '+ Create Credentials'. Choose 'OAuth client ID' option. On the next screen choose 'Web application' as Application Type.

Once OAuth client is created copy ClientID and Client Secret, you are going to need them to create a REST API connection in PHPRunner or ASPRunner.NET.

In Oauth Client properties add Authorized redirect URIs. For local testing use:

PHPRunner

http://localhost:8086/oauthcallback.php

ASPRunner.NET

http://localhost:8086/oauthcallback

For production use:

PHPRunner

https://server.com/path/oauthcallback.php

ASPRunner.NET

https://server.com/path/oauthcallback

2. Enable Google Drive API

Under 'Library' find 'Google Drive API', click it and then click 'Enable'.

2. Create REST API connection

Proceed to PHPRunner or ASPRunner.NET and add a new REST API connection to your project. Choose 'OAuth 2.0 - User' as the authorization method.

Paste your saved Client ID and Client Secret there. The rest of the settings should be as specified below.

Authorization URI

https://accounts.google.com/o/oauth2/v2/auth

Access token URI

https://oauth2.googleapis.com/token

Scope

https://www.googleapis.com/auth/drive.readonly

More info: Google Drive API reference

3. Enable List operation

This is what you need to add to the Resource box:

/files?<?q=name%20contains%20':{all_field_search}'?>

4. Adding a list of fields

Since this API uses OAuth - User type of authentication we cannot run the request in the software and have to specify a list of fields manually. We have pulled the list of available fields from the Google Driver REST API docs. This is how a single field setup looks:

For simplicity purposes, we use 'string' type for all fields. Here is the list of all available fields: name, id, description, trashed, starred, mimeType, hasThumbnail, thumbnailLink, modifiedTime, createdTime, webContentLink, webViewLink.

5. Enable Single operation

This is what you need to add to the Resource box:

/files/:{filter.id}?fields=*

This is pretty much it. Once you build and run your application you will be presented with a Google Account login window. You can logon or choose one from your Google Accounts once once you are in you will see a list of files stored on your Google Drive.

2 thoughts on “Working with Google Drive REST API

Leave a Reply

Your email address will not be published. Required fields are marked *