Google 日历 API 获取一个事件

Google Calendar API get an event

我想根据它的 ID 从 google 日历中获取一个事件并更新其中的一些数据。

$client = $this->getClient();
$service = new Google_Service_Calendar($client);
$calendarId = 'primary';
$optParams = array(
    'orderBy' => 'startTime',
    'singleEvents' => true,
    'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
$events = $results->getItems();

上面的代码适用于所有事件和作品。

$client = $this->getClient();
$service = new Google_Service_Calendar($client);
$calendar = $service->calendarList->get($calendarId);

上面的代码是针对单个事件的,我在请求时收到错误。据我所知,我需要第二个参数,例如 $optParams ,但我不确定要赋予该参数什么值。

URL for google doc.

错误信息。

PHP Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}
 in ..\vendor\google\apiclient\src\Google\Http\REST.php:123
Stack trace:
#0 ..\vendor\google\apiclient\src\Google\Http\REST.php(98): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 ..\vendor\google\apiclient\src\Google\Task\Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 ..\vendor\google\apiclient\src\Google\Http\REST.php(61): Google_Task_Runner->run()
#3 C:\xampp\sites\calmnest\wp-content\plugins\nrc- in ..\vendor\google\apiclient\src\Google\Http\REST.php on line 123

回答

您收到错误消息是因为您没有使用正确的方法

如何更新活动

  1. Get 事件
  2. 修改活动
  3. Update 事件

两种方法都使用两个查询参数:calendarIdeventId:

  • calendarId: 你可以写 primary 或你 gmail 帐户。要查看您帐户中所有可用的日历,您可以列出它们:CalendarList: list
  • eventId: 您要更新的事件的id。您可以列出特定日历中的所有可用事件:Events: list

代码

$calendarId = 'primary';
$eventId = 'eventId'; 

// Get Event
$event = $service->events->get($calendarId, $eventId); 

// Modify Event
$event->setSummary('New title');
$event->setDescription('New describtion');

// Update Event
$updatedEvent = $service->events->update($calendarId, $eventId, $event);