尝试使用服务帐户在谷歌日历中创建一个事件



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

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

 <?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:xampphtdocssvgoogle-api-php-client-mastersrcGoogleHttpREST.php:79
      Stack trace:
      #0 C:xampphtdocssvgoogle-api-php-client-mastersrcGoogleHttpREST.php(44):              Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
      #1 C:xampphtdocssvgoogle-api-php-client-mastersrcGoogleClient.php(504):  Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
      #2 C:xampphtdocssvgoogle-api-php-client-mastersrcGoogleServiceResource.php(195): Google_Client-     >execute(Object(Google_Http_Request))
      #3 C:xampphtdocssvgoogle-api-php-client-mastersrcGoogleServiceCalendar.php(1459): Google_Service_Resource->call('insert', Array, 'Google_Service_...')
      #4 C:xampphtdocssvtest.php(50): Google_Service_Calendar_Events_Resource->insert('primary', Object(Google_Service_Calendar_Event))
      #5 {main in C:xampphtdocssvgoogle-api-php-client-mastersrcGoogleHttpREST.php on line 79

我认为您缺少请求授权部分:

    //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

相关内容

  • 没有找到相关文章

最新更新