GoogleCalendarAPI accept / reject event

I am working on the GoogleCalendar API and am using node.js as a platform to build my application. I can create events using the authentication procedure and create a calendar event using the access token generated during authentication.

My question is, if we have any participant in the event and I want to accept / reject the event using the calendar API from the participant, how can we do this?

I tried to retrieve the participant’s calendar event and match it with the iCalUID of the event that was originally created, and then changed the event using the update event in the participant’s calendar.

+5
source share
5 answers

The event creator or owner cannot modify the participants response. Only members can change their status.

To update the status on the user side, you can use the Event.update API and provide a value for 'attendees.responseStatus'. The participant’s response status has 4 (four) possible values ​​(described below).

'needsAction' - did not respond to the invitation. "declined" - declined the invitation. "preliminary" - conditionally accepted the invitation; "accepted" - accepted the invitation.

In addition to this, you can use the word "primary" as a value for the calendar identifier to represent the current user in the system

CalendarId: Calendar ID. To get the calendar IDs, call the calendarList.list method. If you want to access the main calendar of the current user, use the keyword "primary". (Line).

For the identifier, you need to use the "id" returned by the Events.list API, not the "iCalUID". The two are different from each other as described here .

Other fields that you need to provide are the email (member), start and end dates.

For more information, you can view the official documentation, the link below: https://developers.google.com/google-apps/calendar/v3/reference/events

+2
source

Here is an example in java using PATCH. Create an event object using only the information that you want to change, in this case the visitor and the response status. This code works as a member.

final Event event = new Event() .setAttendees(Arrays.asList(new EventAttendee().setEmail(email) .setResponseStatus("declined"))); try getCalendarService(googleAccountCredential).events() .patch(CALENDAR_PRIMARY, calendarEventId, event) .setSendNotifications(true) .setOauthToken(googleAccountCredential.getToken()).execute(); return true; } catch (final Exception ex) { ... return false; } } 
+1
source

To answer, you need to receive an event with the same event identifier from the participant’s calendar, and then perform a patch or update operation, changing the response status of this participant from needAction to accepted / rejected.

A little documentation on how events are copied between participants and organizers: https://developers.google.com/google-apps/calendar/concepts/sharing

0
source

Here is a Python example for Google Calendar Api v3. You can use update or patch. They both work.

 all_attendees = event['attendees'] event['attendees'] = [{ 'email': ' you@example.com ', 'self': True, 'responseStatus': 'accepted', 'additionalGuests': 0, }] updated_event = service.events().patch(calendarId=calendar_id, eventId=event_id, body=event).execute() 

Have some fun

0
source

As the Android Enthusiast discussed, only a member can update their calendar from a member. You should also check the documentation as he suggested. The answer below is a working example for node.js and python

To update an event, you need to have an event identifier and user email. Get an event from the calendar (with event ID), go through all the participants, change the responseStatus for that particular participant, and then update the Google calendar

For JS host using Google API

 const { google } = require('googleapis'); const calendar = google.calendar({ version: 'v3', auth: 'YOUR-API-KEY-HERE' }); #get the event to be updated let theEvent = calendar.events.get({ calendarId: 'primary', eventId: eventId }) #loop through the whole attendee for (let i = 0, i < theEvent['atendees'].length; i++){ if (theEvent['atendees'][i]['email'] === userEmail){ theEvent['atendees'][i]['responseStatus'] = 'accepted' } } #update the google event calendar.events.update({ calendarId: 'primary', eventId: theEventId, body: theEvent}, function(err, event) { if (err) {  console.log('there was an error');  return; } console.log('Event updated'); }); 

For python using googleapiclient

 from googleapiclient.discovery import build calendar = build('calendar', 'v3', credentials=credential) event = calendar.events().get(calendarId='primary', eventId='eventId').execute() For attendee in event['atendees']: if atendee['email'] == user_email: attendee['responseStatus'] = 'accepted' break #update the google event udated_event = calendar.events().update(calendarId='primary', eventId=eventId, body=event).execute() 
0
source

Source: https://habr.com/ru/post/1240052/


All Articles