Access Google Drive through Rest using PHP

The Google Drive Api let you create apps  for a wide range of devices that access files stored in Google Drive, like read, write, and sync files stored in Google Drive from your own mobile(android , ios) and web apps (php, python , javascript).

Prerequisites

  • PHP (5.4 or greater)
  • Composer (dependency management tool)
  • Google account with google drive enabled
  • web server (XAMPP)

This tutorial is also intended for beginners

Turn on the Drive API

First of all , create a new console project and automatically enable the Drive API. Once the project is created , you must enable the drive api in your project.
To go quick , click the button enable the drive api here , and download the credentials.json file.
Here is where you will find all your project credentials.

In this how-to , we are using OAuth 2.0 login for authentication (there is another way to manage this like api key or service account key)

Create Project

Go to the public web directory of your web server and create new folder . Inside the folder run the command bellow and fill the composer project informations :

$ composer init

Require the Google drive Library in your project . Run :

$ composer require google/apiclient:^2.0

Generate the auto loader file by running :

$ composer dump-autoload

The above command create a file in the vendor directory named autoload.php (read more about composer if things are not clear at this point)

Authentication and authorization principle

The first step is to solicit for a permission about the application you are creating to have access to drive data . if you accept the permission , an authentication code is returned to callback url you specified .if not , an error reponse will be return (you have to manage the reponse behavior by your own application).

if(isset($_GET['code']){
  ... do something 
}

After you get the code , you should to autenticate the client using the returned code to get the token .

$client->authenticate($_GET['code']);

You can retrieve the access token with the getAccessToken method

$access_token = $client->getAccessToken();

Calling Google APIs

Use the access token to call Google APIs by completing the following steps:

  • If you need to apply an access token to a new Google_Client object—for example, if you stored the access token in a user session—use the setAccessToken method:
$client->setAccessToken($access_token);
  • Build a service object for the API that you want to call. You build a a service object by providing an authorized Google_Client object to the constructor for the API you want to call. For example, to call the Drive API:
$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getItems();

Complete example

Check the project in github here : google-drive-demo

The Demo contain only one functionality that list the files in your drive . for more check the google drive api here and enjoy .

inferom
web developer passionate about new technologies