title: Google Drive access for Service account
body: <p>I am trying to access my Google Drive files from a php script that will be run in a cron job, but I can't see my files.
  I am using a Service account set up in Google Developpers console, which do not need authorisation interaction with the user (me).
  This is my code:</p>
  
  <pre><code>require_once 'Google/Client.php';
  require_once 'Google/Service/Drive.php';
  $client_id = '260186860844-kg9uiqe7pjms3s54gabqfnph0iamjdjn.apps.googleusercontent.com';
  $service_account_name = '260186860844-kg9uiqe7pjms3s54gabqfnph0iamjdjn@developer.gserviceaccount.com';
  $key_file_location = 'privatekey.p12';
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("Whatever");
  if (isset($_SESSION['service_token'])) {
    $client-&gt;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'),
      $key
  );
  $client-&gt;setAssertionCredentials($cred);
  if($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {
    $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred);
  }
  $_SESSION['service_token'] = $client-&gt;getAccessToken();
  $service = new Google_Service_Drive($client);
  
  $parameters = array(
      //    'maxResults' =&gt; 10,
      );
  $dr_results = $service-&gt;files-&gt;listFiles($parameters);
  foreach ($dr_results as $item) {
      echo $item-&gt;title, "&lt;br /&gt; \n";
  }
  </code></pre>
  
  <p>It seems I am correctly connected (got token, no errors prompted), but I only get one single file listed, which is not part of my Drive, and which is named "How to get started with Drive"!</p>
  
  <p>I am using latest <a href="https://github.com/google/google-api-php-client" rel="nofollow">Google APIs Client Library for PHP</a> , with php 5.4</p>