First, I can add the event from Google API web page
https://developers.google.com/google-apps/calendar/v3/reference/events/quickAdd#try-it successfully by input "calendarId" and "text" parameters
But I am failed to use "quickAdd" API to add event in Google calendar by using PHP.
require_once "google-api-php-client/src/Google/Client.php";
require_once "google-api-php-client/src/Google/Service/Calendar.php";
// Service Account info
$calName = 'dcm2p5gkf3ge1r7k78f0ko28tc#group.calendar.google.com';
$client_id = '759399732845-j93sis8dktkdj00l3b8mn7gjs3ncf6b9.apps.googleusercontent.com';
$service_account_name = '759399732845-j93sis8dktkdj00l3b8mn7gjs3ncf6b9#developer.gserviceaccount.com';
$key_file_location = 'SMSProject-f14380ee82e4.p12';
$client = new Google_Client();
$client->setApplicationName("SMSProject");
$service = new Google_Service_Calendar($client);
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/calendar'), $key);
$client->setAssertionCredentials($cred);
$createdEvent = $service->events->quickAdd('primary', 'Appointment at Somewhere on January 23rd 10am-10:25am');
echo $createdEvent->getId();
Event ID "g69tq9k7s38rfgit1mpnit9bpk" can be echo in the browser but no event is shown in the calendar
If I change
$createdEvent = $service->events->quickAdd('primary', 'Appointment at Somewhere on January 23rd 10am-10:25am');
to
$createdEvent = $service->events->quickAdd($calName, 'Appointment at Somewhere on January 23rd 10am-10:25am');
Event ID cannot be generated and show below error message
Error calling POST https://www.googleapis.com/calendar/v3/calendars/dcm2p5gkf3ge1r7k78f0ko28tc%40group.calendar.google.com/events/quickAdd: (404) Not Found
I need to use "quickAdd" instead of "insert" API. How to solve the problem? Thanks!
I was having the same issue, but then I discovered that it was, in fact, creating the events in the calendar for the Service Account xxxxxxx#developer.gserviceaccount.com instead of my personal calendar.
If you want it to create the events in your personal calendar that you use, go into the settings for your personal calendar and share it with your Service Account xxxxxxx#developer.gserviceaccount.com
then you should be able to use...
$createdEvent = $service->events->quickAdd($calName, 'Appointment at Somewhere on January 23rd 10am-10:25am');
...instead of 'primary'
Related
I want to insert a new event into Google Calendar by using the Google Calendar API.
And the problem is that I want to set the event status to "free" or "busy" based on the input datetime data.
How to change the event status to "Free"? When I create a new event, it will automatically set to "Busy". I couldn't figure it out on how to set to "Free" or "Busy".
How I insert a new event by using Google Calendar API.
$googleClient = new GoogleClient();
$slotCalendarService = GoogleClientCalendar::getSlotCalendar($googleClient->getClient());
$googleEvent = new Event();
$googleEventStart = new EventDateTime();
$googleEventStart->setTimeZone($attrs['timezone']);
$googleEventStart->setDateTime("2021-12-02T10:00:00");
$googleEvent->setStart($googleEventStart);
$googleEventEnd = new EventDateTime();
$googleEventEnd->setTimeZone($attrs['timezone']);
$googleEventEnd->setDateTime("2021-12-02T10:50:00");
$googleEvent->setEnd($googleEventEnd);
$googleEventAttendee = new EventAttendee();
$googleEventAttendee->email = empty($attrs['attendee']) ? '' : $attrs['attendee'];
$googleEvent->description = $description;
$googleEvent->summary = $remark;
$googleEvent->setAttendees($googleEventAttendee);
$slotCalendarService->getCalendarService()->events->insert($slotCalendarService->getCurrentCalendarId(), $googleEvent);
Base on the the code above, there is no param to set the event status to "free" or "busy".
I also follow the document FreeBusy Google Calendar API and it does not have information on how to set event status to "free" or "busy".
When you want to set to "free", please modify as follows.
From:
$googleEvent->setAttendees($googleEventAttendee);
$slotCalendarService->getCalendarService()->events->insert($slotCalendarService->getCurrentCalendarId(), $googleEvent);
To:
$googleEvent->setAttendees($googleEventAttendee);
$googleEvent->transparency = "transparent"; // Added
$slotCalendarService->getCalendarService()->events->insert($slotCalendarService->getCurrentCalendarId(), $googleEvent);
When you don't use $googleEvent->transparency = "transparent"; or you use $googleEvent->transparency = "opaque";, it's "busy".
Reference:
Events: insert
How can I get locations list in Google My Business API. Where I retrieved account list but I can't figure out how to retrieve location.
Here is my code where I am getting accounts list
define('GOOGLE_CLIENT_ID', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('GOOGLE_CLIENT_SECRET', 'XXXXXXXXXXXXX');
// Create Client Request to access Google API
$client = new Client();
$client->setApplicationName('my-app');
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri('https://example.com/callback');
$client->addScope('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/business.manage');
$client->setAccessType('offline'); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->setAccessToken($accessToken);
$service = new \Google_Service_MyBusinessAccountManagement($client);
$accounts = $service->accounts->listAccounts()->getAccounts(); // get accounts
To get google locations,
you can use this PHP My Business file to make things little easier.
First change you scope to ttps://www.googleapis.com/auth/plus.business.manage then include the file and create a object of Google_Service_MyBusiness with you client and then do like this.
$mybusinessService = new Google_Service_MyBusiness($client);
// Get the first account in the accounts array
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();
$account = $accountsList[0];
// Get the first location in the locations array
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
$location = $locationsList[0];
var_export($location);
With this process you can also able to get google reviews.
For more details check this Google Business API documentation.
Hope it can help
"It's correct!!!
I increase this at my code:
$optParams = array(
'readMask' => 'name',
);
$list_accounts_response = $my_business_account->accounts_locations->listAccountsLocations("accounts/114893266195214446586", $optParams);
var_dump($list_accounts_response);
Thank you.."
source : https://github.com/googleapis/google-api-php-client/issues/2213#issuecomment-1042785983
We have a PHP application and when we create an event in Google Calendar using de API, this event is generated without Hangout Link, but if we create the event manually, the event is created with the Hangout Link. The option to create automatically hangouts when we create an event is checked.
The code is:
$event = new Google_Service_Calendar_Event();
$event->setSummary($summary);
$event->setLocation('hangout');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($startDate->format(\DateTime::ISO8601));
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($endDate->format(\DateTime::ISO8601));
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail($this->masterAccount);
$attendees= [];
$attendees[] = $attendee1;
foreach($company->getUsers()->toArray() as $user){
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail($user->getEmail());
$attendees[] = $attendee1;
}
$event->attendees = $attendees;
$createdEvent = $this->google_calendar_service->events->insert($company->getCalendar()->getCalendarId(), $event);
If we use the form https://developers.google.com/google-apps/calendar/v3/reference/events/insert#try-it to create de Event, the hangout link is created without problems.
Any solutions?
Thanks :)
I know this topic is a bit old, but there are many people still struggling with making Calendar API work. After countless forums visited, hours of reading documentation and hitting my head against the wall I finally figured out, the key element is to set requestId.
In the time i've implemented this, this parameter wasn't mandatory and i skipped it.
Thats being said, my request looks like this:
$event = new Google_Service_Calendar_Event(); //creating empty event
$event->setSummary("Staff meeting"); //title
$event->setLocation("Room 101"); //location
$event->setDescription("We will talk about salary raise!"); //description
$event->setColorId(9); //make it shine by setting color
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($startDate->format(DateTime::ISO8601));
$start->setTimeZone('Europe/Warsaw');
$event->setStart($start); //set start
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime($endDate->format(DateTime::ISO8601));
$end->setTimeZone('Europe/Warsaw');
$event->setEnd($end); //set end
$event->setGuestsCanSeeOtherGuests(false); //I dont want others to see who's invited
$event->setGuestsCanModify(false); //I dont want others to modify event
$event->setGuestsCanInviteOthers(false); //I dont want others to invite
$event->setAnyoneCanAddSelf(false); //I allow only people invited by me, not some strangers walking by
//this part should be executed in loop, for every guest
$attendee = new Google_Service_Calendar_EventAttendee();
$attendee->setEmail("staffmeber1#mywebsite.com");
$attendee->setResponseStatus('accepted');
$attendees[] = $attendee;
//When the list is completed, set guests in event
$event->setAttendees($attendees);
//The most important part about creating hangout
$conferenceData = new Google_Service_Calendar_ConferenceData(); //defining we have conference
$req = new Google_Service_Calendar_CreateConferenceRequest(); //requesting google to create video meeting
$req->setRequestId("req_".time()); //some random string/number changed every call. according to docs subsequent calls with the same request id are ignored
$conferenceData->setCreateRequest($req); //set request for videoconference in conference data
$event->setConferenceData($conferenceData); //set conference data in out event
$creator = new Google_Service_Calendar_EventCreator();
$creator->setDisplayName("Big boss");
$creator->setEmail('boss#mywebsite.com');
$event->setCreator($creator);
$event->setStatus('confirmed'); //lets say everyone is attending :D
$result = $this->calendarClient->events->insert($this->calendarObject->getId(), $event, ['conferenceDataVersion' => 1, 'sendUpdates' => "none"]); //dont forget about conferenceDataVersion or every conference data will be ignored
Working with Python here also faced issue, of events not being created with Hangouts. It seems settings can not be changed from program, but as SGC pointed out, I just changed google calendar settings from UI to add hangouts on every event created.
I am trying to integrate google calender APIv3 using php in my site.The idea is each user have a set of events as per his subscribed package on site on different dates.If the user select a new package later all events from previously subscribed package will overwrite with new one.
For me everything works fine except the following.
1)when a user reached gmail calender rendering page ,for the first time successfully created a calender (eg:My first work as name ) on left menu of My calender list and on that all events are inserted (for eg:events on march1,march5,march8 with different titles)
2)When the user click the link from site to render the same calender for the second time it will again created on left menu My calender list (eg:My first work and My first work two calenders with same name ).All events inserted on previous calender insertion will be there and the second time of insertion to same dates also will be there.(For eg: duplication of events on same dates march1,march5,march8 )
What i want is each time when a user click from site to render the calender page the calender should be created as a new one (this is working) and also the calender with same name on left menu "My calender list" should be deleted.So that there may not be duplicate data/event on same date.
any body please help me to find a solution?
following is my code snippet
$service = new Google_Service_Calendar($client);
$calendar = new Google_Service_Calendar_Calendar();
$calendar->setSummary('My first work');
$calendar->setTimeZone( 'Asia/Kolkata');
$createdCalendar = $service->calendars->insert($calendar);
$calendar = $service->calendars->get($createdCalendar->getId());
$calendarList = $service->calendarList->listCalendarList();
foreach($events_data_arr as $key=>$events_data_row)
{
$event = new Google_Service_Calendar_Event();
$event->setSummary($events_data_row['title']);
$event->setLocation('India');
$event->setDescription($events_data_row['description']);
$start = new Google_Service_Calendar_EventDateTime();
$start->setTimeZone('Asia/Kolkata');
$start->setDateTime($events_data_row['start_date'].'T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setTimeZone( 'Asia/Kolkata');
$end->setDateTime($events_data_row['end_date'].'T10:00:00.000-07:01');
$event->setEnd($end);
$createdEvent = $service->events->insert($createdCalendar->getId(), $event);
}
Following code helped me.
$calendarList = $service->calendarList->listCalendarList();
foreach ($calendarList->getItems() as $calendarListEntry) {
$delid = $calendarListEntry->getId();
$calname = $calendarListEntry->getSummary();
if(strtolower(trim($calname)) == "mycalendername")
{
$service->calendarList->delete($delid);
$service->calendars->delete($delid);
//echo "ID= ".$delid ."<br/>";
}
}
The idea is to take all calenders from calenderlist using
$calendarList = $service->calendarList->listCalendarList();
Afetr that take calender id of those calender which have a name equal to our calender name and delete that calender.Only after this step start creating and inserting to our calender "mycalendername".This will prevent duplicate creation of same calender name to user's main calender list and each time taking our code will create a fresh calender with fresh data
I'm using the Google Calendar API to insert events in my customers Google Calendars. I want to insert events in the timezone of their calendar but currently I can only use one timezone via the Google Calendar API.
How can I get the timezone of a specific Google Calendar using the Google Calendar API?
By now I'm using the following code to add an event to a Google Calendar:
$eventName='You have ListingAppointments.com Appointment';
$Address='testing-456';
$Ausers='test48#gmail.com';
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
//print_r($events);
$event = new Google_Event();
$event->setSummary($eventName);
$event->setLocation('testing234');
$start = new Google_EventDateTime();
$start->setDateTime('2013-07-19T10:25:00.000-07:00');
//print_r($start);
$event->setStart($start);
$end = new Google_EventDateTime('2013-07-19T10:25:00.000-07:00');
$end->setDateTime($end);
//print_r($end);
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail($Ausers);
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $cal->events->insert('primary', $event);
print_r($createdEvent);
}
Give this code a try:-$calendar = $cal->calendars->get('primary');echo $calendar->getTimeZone();
line one gets you the primary calendar and line two gets you the timezone using getTimeZone() method.
or simply $cal->calendars->get('primary')->getTimezone();