0

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

4

1 回答 1

1

正如您所提到的,第一步是让 Google Apps 域管理员向您的服务帐户的 ClientID 授予域范围的授权,在您的情况下,用于日历读/写范围,即https://www.googleapis .com/auth/日历

之后,创建一个Google_AssertionCredentials对象并将您要写入其日历的用户的电子邮件地址设置为该对象的子对象。假设mydomain.com是您的服务帐户已被授予权限的域:

$client = new Google_Client();
$client->setApplicationName("Calendar Service Account Test");

$key = file_get_contents(<path_to_your_key>);
$auth = new Google_AssertionCredentials (
  <service_account_email>,
  'https://www.googleapis.com/auth/calendar',
  $key);
$auth->sub = 'account@mydomain.com';
$client->setAssertionCredentials($auth);
$calendar = new Google_CalendarService($client);

您现在可以在account@mydomain.com的日历中编写一个事件,执行如下操作:

$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-09-07T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-09-07T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('myguest@example.com');
$event->attendees = array($attendee1);

$calendar->events->insert('primary', $event);
于 2013-09-06T15:50:50.610 回答