gabrielpreite
gabrielpreite

Reputation: 25

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

Upvotes: 2

Views: 399

Answers (2)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

This method takes a JSON string as its parameter. So if you want to use a dictionary you would need to covert your dictionary to a json string for the request body.

Also you dont need to set id, kind or etag Google creates those values as they are not writeable by you in the request. calendars resource

The documentation gives an example of how to make the call Calendar.insert

calendar = {
    'summary': 'calendarSummary',
    'timeZone': 'America/Los_Angeles'
}

created_calendar = service.calendars().insert(body=calendar).execute()

Upvotes: 1

Iamblichus
Iamblichus

Reputation: 19309

  • 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()

Upvotes: 1

Related Questions