1

This is my first time using Google App Engine, so some of my terminology might be a bit off, bear with me.

I had some php code running in a web app outside of GAE that was using the google-api-php-client library to access gdrive folders and create files. It was working wonderfully.

I'm trying to migrate to GAE with a project that has domain-wide delegation so that my app can access/create files in my gdrive account. For example, let's say my domain is kennywyland.com and my Google account that I'm using is kenny@kennywyland.com.

I create my Google Client like this, which I got from https://developers.google.com/api-client-library/php/auth/service-accounts:

$client = new Google_Client();
$client->setAuth(new Google_Auth_AppIdentity($client));
$client->getAuth()->authenticateForScope('https://www.googleapis.com/auth/drive');

Then I create the gdrive service like this:

$service = new Google_Service_Drive($client);

I didn't get any errors from this and I am able to perform queries without error messages being thrown, however, I'm getting back completely empty result sets. I've got plenty of files in my gdrive account, so I would assume that I should see them when I publish the following code to GAE and run it (the retreiveAllFiles() function is from Google's code example found here: https://developers.google.com/drive/v2/reference/files/list#try-it):

$allfiles = retrieveAllFiles($service);

print_r($allfiles);

function retrieveAllFiles($service) {
    $result = array();
    $pageToken = NULL;

    do {
        try {
            $parameters = array();
            if ($pageToken) {
                $parameters['pageToken'] = $pageToken;
            }
            $files = $service->files->listFiles($parameters);

            $result = array_merge($result, $files->getItems());
            $pageToken = $files->getNextPageToken();
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
            $pageToken = NULL;
        }
    } while ($pageToken);
    return $result;
}

However, I get an empty result set. The print_r() prints an empty array:

Array ( )

What am I missing? I feel like my project/app is able to successfully access the gdrive server, but it's just not able to see any files in my account associated with this work domain.

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229

1 Answers1

0

The solution involved one primary change which required one further change. The primary change was that I was using the google-api-php-client v1 but I needed to be using the v2 release candidate (which is oddly located in the https://github.com/google/google-api-php-client/tree/v1-master branch).

They have a list of code changes needed to upgrade from 1.0 to 2.0 (thank you, very handy githubers!) https://github.com/google/google-api-php-client/blob/master/UPGRADING.md

I ended up configuring my client with the following code and then it was immediately able to access gdrive as if it were the currently logged in person (assuming 'kenny@kennywyland.com' in my example, who is part of my special domain with domain-wide delegation).

require_once('vendor/autoload.php');

$client = new Google_Client();

$client->setAuthConfig('myapp-xxxxxxxx.json');
$user_to_impersonate = "kenny@kennywyland.com";
$client->setSubject($user_to_impersonate);

$client->addScope('https://www.googleapis.com/auth/drive');

$service = new Google_Service_Drive($client);

The myapp-xxxxxxx.json file was generated in my Developer Console for my App's Service Account.

Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229