title: google-api-php-client connect with key.p12
body: <p>I'm able to add event to my google calendar with an API keys and OAuth 2.0 client IDs but I want to do this without the authorization screen of google.</p>
  
  <p>I followed the information found on this post:</p>
  
  <p>stackoverflow.com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication</p>
  
  <p>For this, I created a 'Service account keys'. I went to my project credentials and select 'Create Credentials > Service Account Key'</p>
  
  <p>Then:</p>
  
  <p>service account > my_project_name</p>
  
  <p>Key type > p12</p>
  
  <p>I saved the key file key.p12</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  // display error, debbug
  ini_set('display_errors',1);
  
  session_start();
  
  require_once './google-api-php-client/src/Google_Client.php';
  require_once './google-api-php-client/src/contrib/Google_CalendarService.php';
  
  // following values are taken from : console.developers.google.com/apis/credentials?project=projectname credentials
  // OAuth 2.0 client IDs : Client ID
  const CLIENT_ID = 'xxxx';
  // Service account keys : Service account
  const SERVICE_ACCOUNT_NAME = 'my_project_name';
  
  // key 
  const KEY_FILE = './key.p12';
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("Google Calendar API Quickstart");
  
  if (isset($_SESSION['token'])) {
      $client-&gt;setAccessToken($_SESSION['token']);
  }
  
  // Load the key in PKCS 12 format
  $key = file_get_contents(KEY_FILE);
  
  $client-&gt;setAssertionCredentials(new Google_AssertionCredentials(
      SERVICE_ACCOUNT_NAME,
      array('https://www.googleapis.com/auth/calendar'),
      $key)
  );
  
  $client-&gt;setClientId(CLIENT_ID);
  $cal = new Google_CalendarService($client);
  
  //Save token in session
  if ($client-&gt;getAccessToken()) {
      $_SESSION['token'] = $client-&gt;getAccessToken();
  }
  
  // my code here
  $event = new Google_Event(...
  $calendarId = 'mycalendarID@group.calendar.google.com';
  $event = $cal-&gt;events-&gt;insert($calendarId, $event);
  
  ?&gt;
  </code></pre>
  
  <p>And here is the error message:</p>
  
  <pre><code>Fatal error:  Uncaught Google_AuthException: Error refreshing the OAuth2 token, message: '{
    'error' : 'invalid_client',
    'error_description' : 'The OAuth client was not found.'
  }' in /home/www/google-api-php-client/src/auth/Google_OAuth2.php:288
  Stack trace:
  #0 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(264): Google_OAuth2-&gt;refreshTokenRequest(Array)
  #1 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(218): Google_OAuth2-&gt;refreshTokenWithAssertion()
  #2 /home/www/google-api-php-client/src/service/Google_ServiceResource.php(167): Google_OAuth2-&gt;sign(Object(Google_HttpRequest))
  #3 /home/www/google-api-php-client/src/contrib/Google_CalendarService.php(469): Google_ServiceResource-&gt;__call('insert', Array)
  #4 /home/www/goog in /home/www/google-api-php-client/src/auth/Google_OAuth2.php on line 288
  </code></pre>
  
  <p>It seems that the $client->getAccessToken() isn't set but I don't know why.</p>
  
  <p>Thanks in advance for your help.</p>