Beusebiu
Beusebiu

Reputation: 1523

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.

$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.

URL for google doc.

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

Upvotes: 1

Views: 1900

Answers (1)

fullfine
fullfine

Reputation: 1461

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:

  • calendarId: you can write primary or you gmail account. In order to see all available calendars in your account you can list them with: CalendarList: list
  • eventId: the id of the event that you want to update. 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);

Upvotes: 1

Related Questions