简体   繁体   English

谷歌日历 API 获取活动

[英]Google Calendar API get an event

I want to get an event from google calendar, based on it's ID and to update some data on it.我想从谷歌日历中获取一个事件,基于它的 ID 并更新它的一些数据。

$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();

The code from above is for all events and works.上面的代码适用于所有事件和作品。

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

The code from above is for a single event and I get an error on request.上面的代码是针对单个事件的,我收到请求时出错。 From what I saw I need a second param like $optParams , but I am not sure what value to give to this param.从我所看到的情况来看,我需要第二个参数,例如$optParams ,但我不确定该参数具有什么价值。

URL for google doc. URL 用于谷歌文档。

Error message.错误信息。

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

Answer回答

You are getting an error because you are not using the correct method您收到错误是因为您没有使用正确的方法

How to update an event如何更新事件

  1. Get the event 获取事件
  2. Modify the event修改事件
  3. Update the event 更新活动

Both methods use two query parameters: calendarId and eventId :这两种方法都使用两个查询参数: calendarIdeventId

  • calendarId : you can write primary or you gmail account. calendarId :您可以编写primary帐户或gmail帐户。 In order to see all available calendars in your account you can list them with: CalendarList: list为了查看您帐户中所有可用的日历,您可以使用以下命令列出它们: CalendarList: list
  • eventId : the id of the event that you want to update. eventId :要更新的事件的id You can list all the available events in a particular calendar with:Events: list您可以列出特定日历中的所有可用事件:事件:列表

Code代码

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM