Reputation: 2382
I am trying to get the calendar event so I can then update it as the document shows.
My assumption was that an event with such JSON:
{
'kind': 'calendar#event',
'etag': '"32...000"',
'id': '6oo....jsa',
'status': 'confirmed',
'htmlLink': 'https://www.google.com/calendar/event?eid=Nm...YjhAZw',
'created': '2021-02-25T20:13:18.000Z',
'updated': '2021-02-28T01:21:43.762Z',
'summary': 'code',
'creator': {
'email': '[email protected]'
},
'organizer': {
'email': '[email protected]',
'displayName': 'Website',
'self': True
},
'start': {
'dateTime': '2021-02-27T23:30:00-05:00'
},
'end': {
'dateTime': '2021-02-28T00:00:00-05:00'
},
'iCalUID': '[email protected]',
'sequence': 27,
'reminders': {
'useDefault': True
},
'eventType': 'default'
}
The calendarId would be iCalUID
& the eventId would be id
or even the id after the calendar url. However, trying both of those interchangeably from the google API platform keeps on giving:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}
What should I do different?
My ultimate goal is to modify the event's summary
, startTime & endTime for any event with a calendarID and eventID, hence my code:
def modifying():
credentials = google.oauth2.credentials.Credentials(
**flask.session['credentials'])
flask.session['credentials'] = credentials_to_dict(credentials)
service = build('calendar', 'v3', credentials=credentials)
event = {
'summary': 'modified',
'location': '',
'description': '',
'start': {
'dateTime': '2021-02-27T17:09:25.993663-05:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2021-02-27T21:09:25.993663-05:00',
'timeZone': 'America/Los_Angeles',
}
}
try:
events_result =service.calendarList().list().execute()
events = events_result.get('items', [])
Ids = [item['id'] for item in events] #do this so it can search across all calendars & not just 'primary'
service.events().update(calendarId=Ids, eventId='6oo....jsa', body=event).execute()
return 200
except Exception as err:
print(err)
return {"message":"Server under maintenance"}
P.S: I have already tried all the solutions in the SO post as shown.
Upvotes: 0
Views: 818
Reputation: 1461
To update an event with Python you need to use the method update
of the calendar service:
service.events().update(calendarId=calendarId, eventId=eventId, body=body).execute()
The request body
must contain the start
and end
dates of the event.
primary
for the default calendar)summary
of all the events until you find one that matches your searchid
, start
and end
datessummarySearch = 'old summary'
events = service.events().list(calendarId=calendarId).execute()
for ev in events['items']:
try:
print(ev['summary'])
if ev['summary'] == summarySearch:
eventId = ev['id']
startDate = ev['start']['date']
endDate = ev['end']['date']
except:
print('empty summary')
body request
. It can be easily constructed with Try this API#startDate = '2021-02-16' # uncomment to overwrite
#endDate = '2021-02-18' # uncomment to overwrite
summary = 'new summary'
body = {
'start': {'date': startDate},
'end': {'date': endDate},
'summary': summary
}
result = service.events().update(calendarId=calendarId, eventId=eventId, body=body).execute()
result
to check that the event has been updated successfullyI keep the start
and end
dates because they are mandatory to update an event. If you want to overwrite them is okay and you don't have to store them previously, but I wanted to give a solution that can fit in more situations.
Upvotes: 1
Reputation: 117281
First off you are doing a calendarlist.list
events_result =service.calendarList().list().execute()
Which Returns the calendars on the user's calendar list.
The response from that being a list of calendar resources
You then appear to be trying to update a static event id '6oo....jsa' on the first calendar that is returned by the calendar list response.
service.events().update(calendarId=Ids, eventId='6oo....jsa', body=event).execute()
How exactly do you know that this static event id is even part of that calendar? Which is why you are getting a 404 it does not exist.
Why not do a events.list in order to get a list of the events on the calendar. Or even better if you don't mind getting an error just do a event.get on the calendar for your static event id.
BTW event.list wont return a calendar id in the response as its assumed that you already know the calendar id since you used it go get the events in the first place.
Upvotes: 1