title: access user calendar with service account
body: <p>I have a php application on my intranet that needs to add events in the calenders of my users. </p>
  
  <p>I followed the following instructing
  <a href="https://developers.google.com/+/domains/authentication/delegation" rel="nofollow">https://developers.google.com/+/domains/authentication/delegation</a></p>
  
  <p>I setup the google-api-php client  and used the example service-accounts.php as my starting point.</p>
  
  <pre><code>$client_id = 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.apps.googleusercontent.com';
  $service_account_name = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy@developer.gserviceaccount.com';
  $key_file_location = 'file.p12';
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("egw - intranet");
  $service = new Google_Service_Calendar($client);
  //authenticate
  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();
  
  $event = new Google_Service_Calendar_Event();
  $event-&gt;setSummary('API V3 TEST');
  $event-&gt;setLocation('rrrrrrrrrrrrrrrrrrrrrrrrr');
  $start = new Google_Service_Calendar_EventDateTime();
  $start-&gt;setDateTime('2014-11-03T10:00:00.000-07:00');
  $event-&gt;setStart($start);
  $end = new Google_Service_Calendar_EventDateTime();
  $end-&gt;setDateTime('2014-11-04T10:25:00.000-07:00');
  $event-&gt;setEnd($end);
  $attendee1 = new Google_Service_Calendar_EventAttendee();
  $attendee1-&gt;setEmail('email@domain.com');
  
  $attendees = array($attendee1);
  $event-&gt;attendees = $attendees;
  $createdEvent = $service-&gt;events- &gt;insert('email2@domain.com', $event);
  
  echo $createdEvent-&gt;getId();
  </code></pre>
  
  <p>I'm recieving 404 not found error. 
  If I manually share the calendar I am using for my test with the service account, It works. But I would like for my program to also work without manually sharing each and every calendar of all my users.  Can somebody tell me what I am missing?</p>
  
  <p>Kind regards,</p>