title: Google Service Account Authentication PHP
body: <p>I am trying to authenticate a service account so that I can use the access token with the client JSON_API library.</p>
  
  <p>I have viewed these articles:  </p>
  
  <p><a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php" rel="noreferrer">https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php</a><br>
  <a href="https://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary" rel="noreferrer">https://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary</a></p>
  
  <p><a href="https://developers.google.com/storage/docs/authentication#service_accounts" rel="noreferrer">https://developers.google.com/storage/docs/authentication#service_accounts</a>
  <a href="https://developers.google.com/accounts/docs/OAuth2#scenarios" rel="noreferrer">https://developers.google.com/accounts/docs/OAuth2#scenarios</a></p>
  
  <p>Here's my PHP Code</p>
  
  <pre><code>&lt;?php
  
  require_once 'google-api-php-client/src/Google_Client.php';
  
  const CLIENT_ID = "";
  const SERVICE_ACCOUNT_NAME = "";
  const KEY_FILE = "super secret path of course ;)";
  
  $client = new Google_Client();
  
  // Loads the key into PKCS 12 format
  $key = file_get_contents(KEY_FILE);
  $client-&gt;setAssertionCredentials(new Google_AssertionCredentials(
      SERVICE_ACCOUNT_NAME,
      array('https://www.googleapis.com/auth/prediction'),
      $key
    )
  );
  
  $client-&gt;setClientId(CLIENT_ID);
  $auth = $client-&gt;authenticate();
  print $auth ? "Returned true" : "Returned false";
  print "&lt;br&gt;";
  print is_null($client-&gt;getAccessToken()) ? "It's null" : "Works";
  
  ?&gt;
  </code></pre>
  
  <p>Here's my output:</p>
  
  <blockquote>
    <p>Returned true<br>
    It's null</p>
  </blockquote>