2
votes

I have been trying to implement a program that uploads backups of my user's websites to google drive. All of them have an account on my domain, so I went through the steps of granting domain wde delegation of authority for my app as described here: https://developers.google.com/drive/delegation

Unfortunately their sample code to instantiate a drive service object fails on many levels. Here it is:

<?php

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();

$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'privatekey.p12';

/**
 * Build and returns a Drive service object 
 * authorized with the service accounts
 * that acts on behalf of the given user.
 *
 * @param userEmail The email of the user.
 * @return Google_DriveService service object.
 */
function buildService($userEmail) {
  $key = file_get_contents(KEY_FILE);
  $auth = new Google_AssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      array(DRIVE_SCOPE),
      $key);
  $auth->setPrn($userEmail);
  $client = new Google_Client();
  $client->setUseObjects(true);
  $client->setAssertionCredentials($auth);
  return new Google_DriveService($client);
}

?>

The first obvious error is they have you set up variables but then the function uses constants. So I hardcoded in what should be there for the constants (KEY_FILE, SERVICE_ACCOUNT_EMAIL, etc) just to see if it worked and then I get the following error:

Fatal error: Call to undefined method Google_AssertionCredentials::setPrn()

Does anyone have any suggestions or comments on how to fix this? If you google these issues, google just gives page after page of links to their own documentation, which as I show above, does not work at all.

Basically I was hoping to see an example of how to use a "service account" which has been granted domain wide access to instantiate a drive service object.

1
I also got the issue. I think they defined three variables and using three constants!Mohammed H
yeah, I pointed that out in my question. And I did some searching last night and found that the setPrn method exists nowhere in the entire library. I will test your answer and let you know if it works :). Thanks for the quick response.pathfinder

1 Answers

4
votes

It seems that there are some typos (If we wrote the doc, it should be called bug :) ) in the documentation.

<?php

require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DriveService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();

function buildService($userEmail) {

  $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
  $SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com';
  $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'privatekey.p12';

  $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);

  $auth = new Google_AssertionCredentials($SERVICE_ACCOUNT_EMAIL, array($DRIVE_SCOPE), $key); // Changed!

  $auth->prn = $userEmail; // Changed!

  $client = new Google_Client();
  $client->setUseObjects(true);
  $client->setAssertionCredentials($auth);
  return new Google_DriveService($client);
}

$service = buildService('[email protected]');


$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = "contents";

$createdFile = $service->files->insert($file, array('data' => $data,'mimeType' =>'text/plain',));

print_r($createdFile);
  1. They defined three varivbales but used three three constants- Removed the contsnts and used the variables instead.

  2. There is no method Google_AssertionCredentials::setPrn(). The property prn's visibility is public. So you can set it as $auth->prn = $userEmail;