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?
Related
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!
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.
I'm trying to use Google calendar api and its php client library to make a php function that add an event with the variables i give it through parameters.
But it seems that the documentation is outdated, and i can't find a good tutorial to help me. Here is what I've done for the moment :
<?php
require_once 'google-api-php-client/autoload.php';
session_start();
//require_once 'google-api-php-client/src/Google/Client.php';
//require_once 'google-api-php-client/src/Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("test");
$client->setClientId("xxxx.apps.googleusercontent.com");
$client->setClientSecret("xxxx");
$client->setRedirectUri("http://localhost/");
$client->setDeveloperKey("xxxx");
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-10-16T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-10-16T10:25:00.000-07:00');
$event->setEnd($end);
/*$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;*/
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
?>
And my browser tells me :
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=xxxx: (401) Login Required' in C:\wamp\www\cnsi\google-api-php-client\src\Google\Http\REST.php on line 76
Could someone help me please ?
I recommend that you used libraries of Zend Framework for it.
checkout http://framework.zend.com/manual/1.12/en/zend.gdata.calendar.html
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;
}
}
I have a working script which is creating events in my google calendar - using google-api-php-client. I'm trying to add a gadget to each event it creates with the following code:
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_CalendarService.php";
define('CLIENT_ID', 'xxx...');
define('SERVICE_ACCOUNT_NAME', 'xxx...');
define('KEY_FILE', 'xxx...');
$client = new Google_Client();
$client->setApplicationName("WEB ACCTION");
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,'xxx...',$key));
$client->setClientId(CLIENT_ID);
$service = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary($variable1);
$event->setLocation($variable2 . ", " . $variable3);
$start = new Google_EventDateTime();
$start->setDateTime(date(DATE_ATOM, strtotime($variable4) + $time_offset));
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime(date(DATE_ATOM, strtotime($variable5) + $time_offset));
$event->setEnd($end);
//The not working section
//$gadget = new Google_EventGadget();
//$gadget->setIconLink('xxx...');
//$gadget->setTitle('xxx...');
//$gadget->setHeight('xxx...');
//$gadget->setWidth('xxx...');
//$gadget->setLink('xxx...');
//$gadget->setType('xxx...');
//$gadget->setDisplay('xxx...');
//$event->setGadget($gadget);
$createdEvent = $service->events->insert($g_calendar , $event);
Unfortunately - I can not make it work and I was not able to find any information about creating the gadgets with google-api-php-client. The library includes a Google_EventGadget class and setGadget function but there is no information how to use ithem. Could anyone help me?
I have found the solution - it occures that the google-api-php-client supports only the certified links to the gadget and its icon. It didnĀ“t work until I used the https://www... links.
It works fine even though the hosting I used for tests has no security certificate.
$gadget->setIconLink('https://www...');
$gadget->setLink('https://www....');
$gadget->setDisplay('chip');
$gadget->setWidth('400');
$gadget->setHeight('100');
$event->setGadget($gadget);
$createdEvent = $service->events->insert($g_calendar , $event);
Hope it will save someone some time!