【问题标题】:trying to create a event in google calendar using service account尝试使用服务帐户在谷歌日历中创建事件
【发布时间】:2015-02-03 15:12:57
【问题描述】:

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

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

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

【问题讨论】:

  • “但我无法得到同样的东西”真的没有描述正在发生的事情......
  • 请立即查看错误详情...
  • 您在提问之前是否尝试过搜索错误?重复...
  • 我尝试将日期格式更改为 2014-09-01T10:00:00-06:00 ..现在能够解决问题...我得到了我的活动的活动 ID。但我的日历中没有创建事件???

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


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 2013-12-18
    • 1970-01-01
    • 2017-12-24
    • 2019-06-10
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多