Search code examples
pythonpython-3.xgoogle-apigoogle-calendar-apigoogle-api-python-client

Google Calendar API missing calendar summary


I'm trying to add calendars to Google through a Python script.

Relevant code:

with open("primo1.json") as f:
    cal = json.loads(f.read())
calId = cal[0]["title"]
print(calId)

try:
    resCalendar = service.calendars().get(calendarId=calId).execute()
except:
    newCal = {}
    newCal["timeZone"] = "Europe/Rome"
    newCal["summary"] = str(calId.split(" ", 1)[0])
    newCal["kind"] = "calendar#calendar"
    newCal["id"] = str(calId.replace(" ", ""))
    newCal["etag"] = str(hashlib.md5(bencode.bencode(newCal)).hexdigest())
    #print(newCal["etag"])

    newCal = json.dumps(newCal)
    res = service.calendars().insert(body=newCal).execute()
#print(resCalendar)

Exception:

Traceback (most recent call last):

File "main.py", line 71, in <module>
    main()
  File "main.py", line 60, in main
    res = service.calendars().insert(body=newCal).execute()
  File "/home/gabriel/.local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/gabriel/.local/lib/python3.7/site-packages/googleapiclient/http.py", line 915, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars?alt=json returned "Missing summary.". Details: "Missing summary.">

The summary I add to newCal dict is just a string (in this case "ARCHITETTURA")

Whole code just in case you need it: https://pastebin.com/vQNwjJ0x


Solution

    • You are providing a JSON formatted string to the request body, returned by json.dumps, and you should be providing a dictionary (see insert(body=None)). Because of this, the property summary cannot be found in the request body, and since this property is required (see request body), you are getting the error Missing summary. Therefore, you should remove the line newCal = json.dumps(newCal).
    • kind, id and etag are not writable properties (see Calendar resource representation); they are provided by Google and the values you set will be ignored. Therefore, there's no point in setting these fields in your dictionary.

    Code snippet:

    newCal = {
      "summary": "Your summary",
      "timeZone": "Europe/Rome"
    }
    res = service.calendars().insert(body=newCal).execute()