I have created an event on Google calendar in python using these lines:
service = build('calendar', 'v3', credentials=credentials)
now = datetime.datetime.utcnow().isoformat() + 'Z'
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
event = {
'summary': 'EVENT TEST',
'eventId': 'passengerID',
'colorId': '6',
'location': '800 Howard St., San Francisco, CA 94103',
'description': 'A chance to hear more about Google\'s developer products.',
'start': {
'dateTime': '2025-03-07T09:00:00-08:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2025-03-07T17:00:00-08:00',
'timeZone': 'America/Los_Angeles',
},
'recurrence': [ ],
'attendees': [
{'email': 'lpage@example.com'},
{'email': 'sbrin@example.com'},
],
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10},
],
},
}
event = service.events().insert(calendarId='primary', body=event).execute()
This works. However, when I try to get the same event by ID using this line:
getEvent = service.events().get(calendarId='primary', eventId='passengerID').execute()
I get this error:
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events/passengerID?alt=json returned "Not Found".
Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'Not Found'}]">
It seems it cannot find this event. Do you know what I am doing wrong? Thank you!
The issue is that you're trying to use 'eventId': 'passengerID' when creating the event, but the eventId field is not a valid parameter for inserting an event. Google Calendar API automatically generates an event ID when you create an event unless you explicitly specify it in the request URL.
If you want to specify a custom event ID (passengerID
), you need to provide it in the API request URL like this:
#insert
event = service.events().insert(calendarId='primary', eventId='passengerID', body=event).execute()
#get
getEvent = service.events().get(calendarId='primary', eventId='passengerID').execute()
Update:
Why the above solution didnt work?
The eventId
parameter is not allowed in the body of the insert()
request, nor is it an argument for the insert()
method. However, Google Calendar API does allow setting a custom event ID using the eventId
parameter in the URL when using the update()
method instead of insert()
.
The updated solution:
1. The insert() method does not support eventId as a parameter.
2. The update() method requires an existing eventId, but if no event exists, it creates a new event with that ID.
3. Now, when you call get() with eventId="passengerID", it will correctly retrieve the event.
event_id = "passengerID"
event = {
'summary': 'EVENT TEST',
'colorId': '6',
'location': '800 Howard St., San Francisco, CA 94103',
'description': "A chance to hear more about Google's developer products.",
'start': {
'dateTime': '2025-03-07T09:00:00-08:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2025-03-07T17:00:00-08:00',
'timeZone': 'America/Los_Angeles',
},
'attendees': [
{'email': 'lpage@example.com'},
{'email': 'sbrin@example.com'},
],
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10},
],
},
}
# Use update() instead of insert() to set a custom eventId
event = service.events().update(calendarId='primary', eventId=event_id, body=event).execute()
# Retrieve the event using the same custom eventId
get_event = service.events().get(calendarId='primary', eventId=event_id).execute()
print("Event retrieved:", get_event)