title: Inserting Google Calendar Entries with Service Account
body: <p>I'm trying to use a service account to create entries on a Google calendar. I'm really close on this, but the very last line won't work. I get a <code>500 Internal Service Error</code> when I let this run. Otherwise, the program runs error free, for whatever that is worth.</p>
  
  <p>The <code>Calendar.php</code> file contents can be found <a href="https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Calendar.php" rel="noreferrer">here</a>. The <code>insert()</code> method that I am trying to call begins on line 1455 of that file. </p>
  
  <pre><code>&lt;?php
  
  function calendarize ($title, $desc, $ev_date, $cal_id) {
  
      session_start();
  
      /************************************************
      Make an API request authenticated with a service
      account.
      ************************************************/
      set_include_path( '../google-api-php-client/src/');
  
      require_once 'Google/Client.php';
      require_once 'Google/Service/Calendar.php';
  
      // (not real keys)
      $client_id = '843319906820-jarm3f5ctbtjj9b7lp5qdcqal54p1he6.apps.googleusercontent.com';
      $service_account_name = '843319906820-jarm3f5ctbtjj7b7lp5qdcqal54p1he6@developer.gserviceaccount.com';
      $key_file_location = '../google-api-php-client/calendar-249226a7a27a.p12';
  
  //    echo pageHeader("Service Account Access");
      if (!strlen($service_account_name) || !strlen($key_file_location))
          echo missingServiceAccountDetailsWarning();
  
      $client = new Google_Client();
      $client-&gt;setApplicationName("xxxx Add Google Calendar Entries");
  
      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/calendar'), 
          $key
      );
      $client-&gt;setAssertionCredentials($cred);
      if($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {
          $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred);
      }
      $_SESSION['service_token'] = $client-&gt;getAccessToken();
  
      // Prior to this, the code has mostly come from Google's example
      //     google-api-php-client / examples / service-account.php
      // and relates to getting the access tokens. 
  
      // The rest of this is about setting up the calendar entry.
      //Set the Event data
      $event = new Google_Service_Calendar_Event();
      $event-&gt;setSummary($title);
      $event-&gt;setDescription($desc);
  
      $start = new Google_Service_Calendar_EventDateTime();
      $start-&gt;setDate($ev_date);
      $event-&gt;setStart($start);
  
      $end = new Google_Service_Calendar_EventDateTime();
      $end-&gt;setDate($ev_date);     
      $event-&gt;setEnd($end);
  
      $calendarService = new Google_Service_Calendar($client);
      $calendarList = $calendarService-&gt;calendarList;
  
      $events = $calendarService-&gt;events;
  
      // if I leave this line, my code won't crash (but it won't do anything, either)
      //echo "here"; die(); 
  
      $events.insert($cal_id, $event, false);
  } 
  
  ?&gt;
  </code></pre>