First attempt at using Google Api for Calendar with a Service Account and I'm finding mixed answers on wether or not I can use a service account with the Calendar API.
Can anyone comment on if they have been successful in utilizing the Google Calendar API with a service account? If you have, can you also post some sample code that inserts an event into a calendar?
I've gone through all of the pre-requisite steps in my Google Apps account to provide access to my API project, I've got my P12 key downloaded... just having trouble making it work with a service account.
UPDATE - 9/5/13
I've had SOME success with the following code HOWEVER it only works if I add the clientID with permissions directly to my calendar in Google Apps which is not what I want to accomplish. I need to be able to insert into a calendar by having the Google Apps admin grant permission via the API Access area in the Admin Control Panel.
Use Case Information:
I am developing a web application that will insert an event into the Google Apps Calendar given a user's SMTP address. There will be no prompt for authentication and the process will be seamless to the user... so this is basically a use case for the Service Account model from what I've been reading.
I've got a piece of code working but ONLY if I add my ClientID from my API into my calendar's security settings with FULL PERMISSIONS. I need to be able to insert into the calendar by having the Google Apps Domain Admin grant my application authority, which they do on the Manage 3rd Party settings under Admin>Security>Advanced Settings>Manage API client access.
Code that works given I add my ClientID into my Google Apps calendar security for the specified user:
<?php
/* THIS IS THE PATH TO THE GOOGLE LIBRARIES
*/
require_once '../assets/google-api-php-client/src/Google_Client.php';
require_once '../assets/google-api-php-client/src/contrib/Google_CalendarService.php';
define('SERVICE_ACCOUNT_NAME', 'XXXXXXXXXXX@developer.gserviceaccount.com');
define('CLIENT_ID','XXXXXXXX-nt292r9jftv56jh9g5hvcob8od8is182.apps.googleusercontent.com');
define('KEY_FILE', 'XXXXXXXXXX123e906b6340d2ccba173225c55-privatekey.p12');
define('CALENDAR_ID','USER@GOOGLEAPPSDOMAIN.com');
$client = new Google_Client();
$client->setApplicationName("My Application Name");
$client->setClientId(CLIENT_ID);
/* Note: make sure to call $client->setUseObjects(true) if you want to see
* objects returned instead of data (this example code uses objects)
*/
$client->setUseObjects(true);
/*session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
*/
/* Load the key in PKCS 12 format - remember: this is the file you had to
* download when you created the Service account on the API console.
*/
$key = file_get_contents(KEY_FILE);
$cred = new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key);
$client->setAssertionCredentials($cred);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* ------------------------- We are now properly authenticated ------------------- */
$cal = new Google_CalendarService($client);
date_default_timezone_set('America/New_York');
$start = date(DATE_ATOM,strtotime('now'));
$end = date(DATE_ATOM,strtotime('+1 month'));
$params = array(
'singleEvents' => 'true',
'timeMax' => $end,
'timeMin' => $start,
'orderBy' => 'startTime');
$event_list = $cal->events->listEvents(CALENDAR_ID, $params);
$events = $event_list->getItems();
echo '<table border="1"><thead>';
echo '<th>ID</th><th>SAFE</th><th>Start</th><th>End</th></tr></thead>';
foreach ($events as $event) {
echo '<tr>';
//echo '<td>'.$event->getId().'</td>';
echo '<td>'.$event->getSummary().'</td>';
echo '<td>'.$event->getStart()->getDateTime().'</td>';
echo '<td>'.$event->getEnd()->getDateTime().'</td>';
echo '</tr>';
}
echo '</table>';
?>
Thanks! Josh