Adding Files to Google Drive using PHP

Google Drive has an API which lets web applications write files to a user's Google Drive, and also register to open those files. Here are my notes on using the API via PHP to upload a file to Google Drive.

Google API Project and Google Chrome extension

First, you need to register your application as an API Project in Google's API Console, and enable the "Drive API" and "Drive SDK" services.

In the "API Access" section, generate an OAuth 2.0 client ID (choose "Web application", and enter the URL of the web application). This should provide a client ID, a client secret, and a URI to which OAuth authentication requests will redirect after confirmation.

In the "Drive SDK" section, fill in all the required information. Use the client ID generated above for the "OAuth Client ID" field. I'm not sure exactly which scopes are needed here, but I've entered "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/userinfo.email" and "https://www.googleapis.com/auth/userinfo.profile".

Next, the App ID from the top of the "Drive SDK" section needs to be added to a Chrome extension. Create a Google Chrome extension for your web application, including the settings "container": "GOOGLE_DRIVE" and "api_console_project_id" (the App ID) in its manifest.json, and publish it on the Chrome Web Store.

When a user installs this Chrome extension, the web application asks for permission to access the user's Google Drive space.

PHP Client Library for Google APIs

Google provide an API Client Library in PHP, and if you checkout the current version of the code there is a newly-added apiDriveService class. At the time of writing it still has a few bugs, so this patch needs to be applied.

I've used the API Client Library in a simple PHP script to upload a PDF file to Google Drive. The steps involved are described below.

To use the client library, put the src folder in your include path, and include the appropriate files in a PHP script:

require 'google-api/apiClient.php';
require 'google-api/contrib/apiOauth2Service.php';
require 'google-api/contrib/apiDriveService.php';

Initialise the client, using the OAuth client ID, client secret and client redirect URI generated in the API console:

$client = new apiClient();
$client->setUseObjects(true);
$client->setAuthClass('apiOAuth2');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
$client->setRedirectUri($config['client_uri']);

To authenticate the client for this user, first generate an authentication URL:

print $client->createAuthUrl(array('https://www.googleapis.com/auth/drive.file'))

Visit that URL in a web browser, and confirm that the web application is allowed to access your Google Drive files. Copy the code from the end of the URL to which you are redirected after confirmation, and use it as $_GET['code'] to retrieve an access token:

$_GET['code'] = ''; // insert the verification code here
file_put_contents('token.json', $client->authenticate());

Use the stored access token to authenticate the client each time it runs:

$client->setAccessToken(file_get_contents('token.json'));

Create a new Google Drive file, attach the raw data (from "test.pdf" in this case) and upload it to the Google Drive service:

$file = new DriveFile;
$file->setMimeType('application/pdf');
$file->setTitle('test.pdf'); // it's important that the title includes the correct file extension

$service = new apiDriveService($client);
$insertedFile = $service->files->insert($file, array('data' => file_get_contents('test.pdf'), 'mimeType' => 'application/pdf'));

The file should now be available in Google Drive.