Daniyal dehleh
Daniyal dehleh

Reputation: 2382

Google calendar API get to update keep on giving 404

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

Answers (2)

fullfine
fullfine

Reputation: 1461

Solution

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.

How to update an event having the summary

  1. List all the events on a specific calendar (primary for the default calendar)
  2. Check the summary of all the events until you find one that matches your search
  3. Keep the id, start and end dates
summarySearch = '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')
  1. Define the new summary (and all the fields to update)
  2. Define the body request. It can be easily constructed with Try this API
  3. Update the event
#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()
  1. Print the result to check that the event has been updated successfully

Some notes

I 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.

Reference

Upvotes: 1

Linda Lawton - DaImTo
Linda Lawton - DaImTo

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

enter image description here

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

Related Questions