1

I'm learning the Drive API and have read all these docs so far. I'm just trying to make an authenticated call to list the files in drive.

This is for an internal application that doesn't need user auth, I just need access to our company drive. So a "Service Account" sounds like the right thing.

Here's what I've done so far

Here's my code for listing files

<?php

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);

    require_once 'google-api-php-client/autoload.php';

    $client_id = '...'; //Client ID
    $service_account_name = '...'; //Email Address
    $key_file_location = '...'; //key.p12

    $client = new Google_Client();
    $client->setApplicationName("...");

    $service = new Google_Service_Drive($client);

    if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
    }

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name,
        array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file'),
        $key
    );
    $client->setAssertionCredentials($cred);
    if ($client->getAuth()->isAccessTokenExpired()) {
      $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    $_SESSION['service_token'] = $client->getAccessToken();

    $results = $service->files->listFiles();

    debug($results);

?>

My results are a single file called "How to get started with Drive". So obviously it isn't authenticating with my account to show my drive files.

Any idea how to get this service account associated with my drive account so I can get a list of files?

Sean Clark
  • 1,436
  • 1
  • 17
  • 31
  • try to trace the http traffic to see what's going on. Also fyi, you don't need drive.file scope if you're also specifying drive as drive is a superset of drive.file. – pinoyyid Feb 18 '15 at 15:56
  • What am I looking for? I know something is not setup right with the user I want to access drive from. – Sean Clark Feb 18 '15 at 18:47
  • It was a general comment. Whenever I've hit Drive or auth problems, it's generally been easier to diagnose once I eliminated the SDK by observing the underlying http. I've always avoided Service Accounts by organising direct access to the specific Drive account that I want to access. This post shows how to use that technique http://stackoverflow.com/questions/19766912/how-do-i-authorise-a-background-web-app-without-user-intervention-canonical?s=7|2.2373 – pinoyyid Feb 18 '15 at 19:39

1 Answers1

1

As you discovered, you are listing the files in the Service Account's Drive. A Service Account can be thought of as a user with its own Drive and it isn't tied to your user account.

If you want to access the files of another user, you must do one of the following:

  1. have the user share the files with the service account
  2. authenticate as that user instead of the Service Account
  3. if you are part of a domain, have a domain administrator delegate authority to this service account to access the files for all users of that domain: https://developers.google.com/drive/web/delegation
Jason Clawson
  • 1,027
  • 7
  • 9
  • Thanks a lot for this info: it saved my day! I have been reading tons of docs by google and still couldn't find a clue about how to do this. – polaretto Feb 11 '16 at 09:50