Google Calendar PHP API, what am I doing wrong? - php

<?php
session_start();
require_once "Google/Client.php";
require_once "Google/Service/Calendar.php";
$apiClient = new Google_Client();
$apiClient->setApplicationName("My Application");
$apiClient->setDeveloperKey("API_KEY");
$service = new Google_Service_Calendar($apiClient);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('EMAIL');
$attendees = array($attendee1,
// ...
);
$event->setAttendees($attendees);
$recurringEvent = $service->events->insert('primary', $event);
echo $recurringEvent->getId();
?>
Everything compiles correctly until I execute $service->events->insert? am I authenticating wrong? I have been trying this for days. Alls I'm trying to do is simply authenticate, and create a calendar event. (I don't need multi-user auth, just one admin user).
Help is appreciated!

Related

Want to create Google Clanedar event which repeats yearly

I successfully managed to add event to google calendar through php api client, but what i want is to make that event repeat every year , such as company anniversay etc, i didn't find that in the google documentation also ,so what should be added while creating event for repeatation
There is documentation for recurring event
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$event->setRecurrence(array('RRULE:FREQ=YEARLY;UNTIL=20110701T170000Z'));
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('primary', $event);
echo $recurringEvent->getId();

How to insert event into a private Google Calendar with PHP?

I'm trying to add new events into my private Google Calendar, but I get (403) Forbidden message. I've searched the internet but I found no answer to my issue.
My scope is set to read/write, and the credentials should be ok, because I can read out all of my events so the connection works fine.
Here's the code:
$service_account = $this->service_account;
$json_key = json_decode(file_get_contents($this->json_url));
$scopes = 'https://www.googleapis.com/auth/calendar';
$application_name = 'Calendar';
$client = new Google_Client();
$client->setApplicationName($application_name);
$cred = new Google_Auth_AssertionCredentials($service_account, array($scopes), $json_key->private_key);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$google_service = new Google_Service_Calendar($client);
$calendar_ids = $this->calendar_ids;
$event = new Google_Service_Calendar_Event();
$event->setSummary($service);
$event->setDescription($user);
$event->setLocation($location);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($this->sqlDateToGcalDate($start_date));
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($this->sqlDateToGcalDate($end_date));
$event->setEnd($end);
$google_service->events->insert($calendar_ids[$steed], $event);
$google_service->events->insert($calendar_ids[$location], $event);

Google Api v3 get modified event via watch(web_hook)

Am using Google Api v3 for adding and modifying google events .
most of the thing works fine except am unable to get the changes made in google events
am using the below code :
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/Google/Service/Analytics.php';
require_once 'google-api-php-client/src/Google/Service/Calendar.php';
$client_id = 'my-client_id'; // works without this
$Email_address = 'my-Email_address';
$key_file_location = 'my-key_file_location.p12';
$client = new Google_Client();
$client->setApplicationName("app-name"); // works without random names
$key = file_get_contents($key_file_location);
$scopes = array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly');
$cred = new Google_Auth_AssertionCredentials($Email_address, $scopes , $key);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired())
{
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Calendar($client);
$cals = $service->calendarList->listCalendarList();
$event = new Google_Service_Calendar_Event();
$event->setSummary('04/03 appointment -1');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2015-03-27T11:00:00.000-00:00');
$start->setTimeZone('Asia/Calcutta');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2015-03-27T11:25:00.000-00:00');
$end->setTimeZone('Asia/Calcutta');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('myemail#gmail.com');
$attendees = array($attendee1);
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('randome-id#group.calendar.google.com', $event);
echo $dynamicId = $recurringEvent->getId();
$events = $service->events->listEvents('randome-id#group.calendar.google.com');
echo "<br>start - into web hoot <br>";
$service = new Google_Service_Calendar($client);
$channel = new Google_Service_Calendar_Channel($client);
$channel->setId($dynamicId);
$channel->setType('web_hook');
echo '<br>';
echo $url = 'https://mydomain.com.return.php';
$channel->setAddress($url);
$timetoExpire = time()+3600000;
$optParams = array('ttl' => $timetoExpire);
$channel->setParams($optParams);
try
{
echo "<br>";
$watchEvent = $service->events->watch('randome-id#group.calendar.google.com', $channel);
print_r($watchEvent);
echo "<br>hook applied<br>";
}
catch(Exception $e)
{
echo $e;
echo "web hook not working<br />.";
}
echo "<br>end";
exit;
return response comes properly in https://mydomain.com.return.php but still am not sure how to read the response
and am unsure
what is
[resourceId] =7U94nhlXTXH_tK13-5JIrZTBWU0
[resourceUri] =https://www.googleapis.com/calendar/v3/calendars/randome-id#group.calendar.google.com/events?alt=json
please help me to figure out how to get the modified event date in goggle calender Thanks in advance

How to add an event to another persons calendar using google calendar api?

i am trying to add a event to a particular person's calendar ,
the person is decided based on html form the value is stored in send
The mail is sent but event is not set.
this is my php code..
<?php
require_once 'google-api-php-client-master/autoload.php';
require_once 'google-api-php-client-master/src/Google/Service/Calendar.php';
$client = new Google_Client();
// OAuth2 client ID and secret can be found in the Google Developers Console.
$client->setClientId('######.apps.googleusercontent.com');
$client->setClientSecret('###');
$client->setRedirectUri('https://www.example.com/oauth2callback');
$client->addScope('https://www.googleapis.com/auth/calendar');
$send = $_POST["to"];
echo "Welcome $send successfully set an event in your calendar";
if( $send == "abc")
{
$to = "abc#gmail.com";
$subject = "Sample Mail Project";
$txt = $_POST["txt"];
$headers = "From: xyz#gmail.com" . "\r\n" ;
mail($to,$subject,$txt,$headers);
$event = new Google_Service_Calendar_EventDateTime();
$event->setSummary('Appointment');
$event->setLocation('VIZAG');
$start = new Google_Service_Calendar_Event();
$start->setDateTime('2014-12-19T06:00:00');
$event->setStart($start);
$start = new Google_Service_Calendar_Event();
$end->setDateTime('2014-12-19T10:10:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('abc#gmail.com');
// ...
$attendees = array($attendee1
// ...
);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
}
According to the documentation you should do an event insert this way:
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
Or you can do it with the quickAdd() method:
$createdEvent = $service->events->quickAdd(
'primary',
'Appointment at Somewhere on June 3rd 10am-10:25am'
);

How to update google calendar event datetime using API V3

I can successfully update the title of the events using Google API V3, but once I include the datetime, I get internal server error. Here's the snippet of my code:
$event = $service->events->get($calendar_id,$event_id);
$event->setSummary($notes);
$service->events->update($calendar_id,$event->getId(),$event);
The above code works well since I'm only updating the title. I'm struggling with the code below:
$event = $service->events->get($calendar_id,$event_id);
$event->setSummary($notes);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-07-22T20:00:00+08:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-07-22T21:00:00+08:00');
$event->setEnd($end);
$service->events->update($calendar_id,$event->getId(),$event);
I'm not sure if I have the correct format for the datetime. Any advice? Thanks!
// Cambio de fecha de evento
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2021-09-01T14:00:00');
$end->setTimeZone('America/Mexico_City');
$event->setEnd($end);
$Start = new Google_Service_Calendar_EventDateTime();
$Start->setDateTime('2021-09-01T12:00:00');
$Start->setTimeZone('America/Mexico_City');
$event->setStart($Start);
$updatedEvent = $service->events->update(CalendarID, $event->getId(), $event);
You probably want to do $event->setEnd($end); instead of $event->setStart($end);

Categories