gpt4 book ai didi

python - Google Calendar API 缺少日历摘要

转载 作者:行者123 更新时间:2023-12-04 08:32:09 26 4
gpt4 key购买 nike

我正在尝试通过 Python 脚本向 Google 添加日历。
相关代码:

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.">
我添加到 newCal dict 的摘要只是一个字符串(在本例中为“ARCHITETTURA”)
完整代码以备不时之需: https://pastebin.com/vQNwjJ0x

最佳答案

  • 您正在向请求正文提供 JSON 格式的字符串,由 json.dumps 返回。 ,并且您应该提供一本字典(请参阅 insert(body=None) )。因此,属性(property)summary在请求正文中找不到,并且由于需要此属性(请参阅 request body ),您收到错误 Missing summary .因此,您应该删除行 newCal = json.dumps(newCal) .
  • kind , idetag不是可写属性(见 Calendar resource representation);它们由 Google 提供,您设置的值将被忽略。因此,在字典中设置这些字段毫无意义。

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

    关于python - Google Calendar API 缺少日历摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64966482/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com