尝试使用服务帐户在Google日历中创建活动

时间:2014-08-27 17:38:33

标签: php google-api google-calendar-api google-oauth

我正在尝试使用服务帐户方法将事件插入到Google日历中。

我可以检索日历列表。但我无法得到同样的东西。请查看我的代码,请帮我解决问题。

 <?php
    //require_once 'google-api-php-client/src/Google/Client.php';
    require_once 'google-api-php-client-master/src/Google/Client.php';
    require_once 'google-api-php-client-master/src/Google/Service/Calendar.php';

    ini_set('display_errors', 1);

    define('CLIENT_ID','XXXXXX');
    define('SERVICE_ACCOUNT_NAME','XXXXXXXX');
    define('KEY_FILE','C:/xampp/htdocs/sv/key.p12');

    $client = new Google_Client();
    $client->setApplicationName("SV Temple APP");
    //$client->setUseObjects(true); //IF USING SERVICE ACCOUNT (YES)

    if (isset($_SESSION['token'])) {
         $client->setAccessToken($_SESSION['token']);
    }

    $key = file_get_contents(KEY_FILE);
    $client->setClientId(CLIENT_ID);

    $client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
             SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/calendar'),
    $key));

    $client->setClientId(CLIENT_ID);
    $cal = new Google_Service_Calendar($client);

    $calList = $cal->calendarList->listCalendarList();

    print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";



    $event = new Google_Service_Calendar_Event();
    $event->setSummary('Appointment');
    $event->setLocation('Somewhere');
    $start = new Google_Service_Calendar_EventDateTime();
    $start->setDateTime('2014-08-27T10:00:00');
    $event->setStart($start);
    $end = new Google_Service_Calendar_EventDateTime();
    $end->setDateTime('2011-08-27T10:25:00');
    $event->setEnd($end);
    $attendee1 = new Google_Service_Calendar_EventAttendee();
    $attendee1->setEmail('xxxxxxx@gmail.com');
    $event->attendees = $attendee1;
    echo "<pre>";
    var_dump($cal);
    $createdEvent = $cal->events->insert('primary', $event);

    echo $createdEvent->getId();
    ?>

我收到的错误就像....

       Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error             calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events: (400) The specified time range is empty.' in C:\xampp\htdocs\sv\google-api-php-client-master\src\Google\Http\REST.php:79
      Stack trace:
      #0 C:\xampp\htdocs\sv\google-api-php-client-master\src\Google\Http\REST.php(44):              Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
      #1 C:\xampp\htdocs\sv\google-api-php-client-master\src\Google\Client.php(504):  Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
      #2 C:\xampp\htdocs\sv\google-api-php-client-master\src\Google\Service\Resource.php(195): Google_Client-     >execute(Object(Google_Http_Request))
      #3 C:\xampp\htdocs\sv\google-api-php-client-master\src\Google\Service\Calendar.php(1459): Google_Service_Resource->call('insert', Array, 'Google_Service_...')
      #4 C:\xampp\htdocs\sv\test.php(50): Google_Service_Calendar_Events_Resource->insert('primary', Object(Google_Service_Calendar_Event))
      #5 {main in C:\xampp\htdocs\sv\google-api-php-client-master\src\Google\Http\REST.php on line 79

1 个答案:

答案 0 :(得分:1)

我认为你错过了请求授权部分:

    //Request authorization
    $authCode = $_GET[ 'code' ];

    // Exchange authorization code for access token
    $accessToken = $client->authenticate($authCode);
    $client->setAccessToken($accessToken);

https://developers.google.com/google-apps/calendar/instantiate