Google Calendar API / Empty the trash of primary calendar - php

I synchronize a calendar with an external application. I have to have the same events IDs in this application and in Google Calendar. But in this application, when an event is deleted, its ID is available for a future new event. So I'd like that, when I delete an event with Calendar API, the event's ID is available. But it always stay in the trash and I can't empty it with API.
So, the two solutions I found are:
Use the clear function:
$cld = $service->calendars->get($calendarId);
$calendarList = $service->calendarList->listCalendarList()->getItems();
$summary = $cld->getSummary();
$timeZone = $cld->getTimeZone();
$events = $service->events->listEvents("primary")->getItems();
$service->calendars->clear('primary');
$cld->setSummary($summary);
$cld->setTimeZone($timeZone);
foreach($events as $event){
$service->events->insert("primary", $event);
}
But this code return message "Forbidden" with code 403 on line $service->calendars->clear('primary');
Create a new calendar and make it primary
$cld = $service->calendars->get($calendarId);
$calendarList = $service->calendarList->listCalendarList()->getItems();
$summary = $cld->getSummary();
$timeZone = $cld->getTimeZone();
$events = $service->events->listEvents("primary")->getItems();
$calendarListEntry = new Google_Service_Calendar_CalendarListEntry();
$calendarListEntry->setId("account_mail#gmail.com");
$calendarListEntry->setSummary($summary);
$calendarListEntry->setTimeZone($timeZone);
$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
foreach($events as $event){
$service->events->insert("account_mail#gmail.com", $event);
}
But this code return message "Invalid Value" with code 400 on line $createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
If someone has an idea for me, it would be amazing!
Thank you.

Related

How can I create an event in google calendar and not have it on trash?

How can I create an event in google calendar and not have it on trash?
I was researching on Google Calendar API ( https://developers.google.com/calendar/v3/reference/events/insert ) how to create an event on the calendar, the insert works fine, the API returns with status: 'confirmed'.
But in the calendar, the event does not appear.
I looked at Google Calendar API and there the status is "canceled", so my events appear on the trash.
I'm not sure of what is happening, someone knows how to create an event on google calendar that does not goes to trash?
<?php
class Calendar
{
private $calendarId = 'primary';
private $service = null;
private $client = null;
public function __construct()
{
$this->client = new Client([
'scopes' => [
Google_Service_Calendar::CALENDAR,
Google_Service_Calendar::CALENDAR_READONLY,
Google_Service_Calendar::CALENDAR_EVENTS,
Google_Service_Calendar::CALENDAR_EVENTS_READONLY,
]
]);
$this->service = new Google_Service_Calendar($this->client->getClient());
}
public function eventAdd()
{
$email = 'seuemail#gmail.com';
$event = new Google_Service_Calendar_Event();
$event->setSummary('Vamos ver agora ;)');
$event->setStatus('confirmed');
$start_datetime = new Google_Service_Calendar_EventDateTime();
$start_datetime->setDateTime('2019-10-10T15:00:00.000-03:00');
$start_datetime->setTimeZone('America/Sao_Paulo');
$event->setStart($start_datetime);
$end_datetime = new Google_Service_Calendar_EventDateTime();
$end_datetime->setDateTime('2019-10-10T20:00:00.000-03:00');
$end_datetime->setTimeZone('America/Sao_Paulo');
$event->setEnd($end_datetime);
$reminder = new Google_Service_Calendar_EventReminder();
$reminder->setMethod('popup');
$reminder->setMinutes(10);
$reminders = new Google_Service_Calendar_EventReminders();
$reminders->setUseDefault(false);
$reminders->setOverrides(array($reminder));
$event->setReminders($reminders);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail($email);
$attendee1->setResponseStatus('accepted');
$attendees = array($attendee1);
$event->attendees = $attendees;
$optParams = array('sendNotifications' => true, 'maxAttendees' => 1);
$event = $this->service->events->insert($this->calendarId, $event, $optParams);
var_dump($event);
}
}
There is an open issue for this problem in Issue Tracker.
As a workaround, you can use your actual calendar ID instead of just 'primary'. This small change should solve your issue.
I hope this helps!

Update Google Calendar Event TimeZone with PHP

I must be missing something in the api docs, how do I update a Google event timezone via an api request?
$service = new Google_Service_Calendar($client);
$event = $service->events->get($calendarId, $eventId);
$event->setSummary($summary);
$service->events->update($calendarId, $event->getId(), $event);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($dateTimeStr);
$event->setStart($start);
$event->setTimezone('America/Los_Angeles');
The error message:
Attempted to call an undefined method named "setTimezone" of class "Google_Service_Calendar_Event". (500 Internal Server Error)
You have to set the timezone on the calendar itself. Don't set it on the event.
See the examples in the documentation:
$calendar = $service->calendars->get('primary');
$calendar->setTimeZone('America/Los_Angeles');
$updatedCalendar = $service->calendars->update('primary', $calendar);

Google Calendar sendnotifications not working

I am modifying a Wordpress plugin (gravity forms to google calendar) to add a specific attendee. The event posts to the calendar, and when I view the event, it includes the intended attendee, however, it is not sending the notification for the event to the attendee.
Here is the snippet:
$event->setStart($start);
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('42fjsjjpte2n7ahlld603600l4#group.calendar.google.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$optArgs = array('sendNotifications'=>true);
if (! empty($location))
$event->setLocation($location);
$ret = self::$client->insert_event($calendar_id, $event, $optArgs);
Can you spot what I've done wrong?

Adding events in bulk to Google Calendar API with PHP

What is the best way I could add possibly 10,000 to 20,000 events at once to a Google Calendar with PHP?
If I try adding them separately I get a "limit exceeded" error far before the daily max.
I've also been looking at importing ICAL/ICS files but I also can't seem to find a function for this besides the calendar subscriptions. Calendar Subscriptions are great but I need to have almost "real-time" updates in my calendars when an event gets changed and it's unclear when calendar subscriptions get updated this could take as much as a day from the initial change in the ICS file.
Need to use Batch Requests. A batch request consists of multiple API calls combined into one HTTP request. Batch processing is a known feature request and is actively worked on. Read the following article about it: https://developers.google.com/google-apps/calendar/batch
See my multipleInsert function for example:
class My_google_calendar
{
...
/** Add single Event for Student */
function addEvent($lesson, $instructor, $return_request = false, $enable_attendees = false)
{
$calendar = $this->getGoogleCalendar(); // get calendar service variable
$lesson_from = date(DATE_RFC3339, $lesson->from);
$lesson_to = date(DATE_RFC3339, $lesson->from + $lesson->duration);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Lesson with student: '$lesson->student_full_name);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($lesson_from);
$start->setTimeZone($this->getGoogleCalendarTimeZone());
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($lesson_to);
$end->setTimeZone($this->getGoogleCalendarTimeZone());
$event->setEnd($end);
$event->setColorId(4);
$description = "...";
$event->setDescription($description);
if (isset($student->email) && $enable_attendees) {
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setResponseStatus('needsAction');
$attendee1->setEmail($student->email);
$attendees = array($attendee1);
$event->setAttendees($attendees);
}
$createdEvent = $this->calendar->events->insert($this->calendar_id, $event, array('fields' => 'id'));
return $return_request ? $createdEvent : $createdEvent->getId();
}
/** Push group of events to the Calendar */
function multipleInsert ($lessons, $instructor)
{
$this->use_batch = true;
$this->client->setUseBatch($this->use_batch);
$batch = new Google_Http_Batch($this->client);
foreach($lessons as $time => $lesson) {
$lesson = array_shift($group['lessons']);
$req = $this->addEvent($lesson, $instructor, true);
$batch->add($req, $time);
}
}
$results = $batch->execute();
return $results;
}
}

Adding a Google Calendar using apiClient

I'm finally able to get an Access Token, now I'm very confused as how to add a Calendar using only Google's provided apiClient.
$apiClient = SiteController::getApiClient();
$service = new apiCalendarService($apiClient);
$calendar = new Calendar();
$calendar->description = "What";
$service->calendars->insert($calendar);
This produces:
Error calling POST https://www.googleapis.com/calendar/v3/calendars?key=mykey: (400) Required
Is there some documentation/examples on adding a Calendar? There are a ton of examples, it seems like, for simply adding an Event.
I'm a little closer now, I get
apiServiceException
Error calling POST
https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey: (404) Not Found
Using the boilerplate code they gave on the documentation
$calendarListEntry = new CalendarListEntry();
$calendarListEntry->setId("calendarId");
$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
echo $createdCalendarListEntry->getSummary();
Inserting a new calendarEntry in google calendar API v3 returns a 404
How do I change my request URL from
https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey
to
https://www.googleapis.com/calendar/v3/calendars
This worked:
// Create new calendar
$apiClient = SiteController::getApiClient();
$service = new apiCalendarService($apiClient);
$calendar = new Calendar();
$calendar->setSummary(Home::model()->count() . '-' . $model->name);
$createdCalendar = $service->calendars->insert($calendar);
You can reference Google Calendar API v1 Developers' Guide.

Categories