Google 日历 API 缺少日历摘要
Google Calendar API missing calendar summary
我正在尝试通过 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)
异常:
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 字典的摘要只是一个字符串(在本例中为“ARCHITETTURA”)
完整代码以备不时之需:https://pastebin.com/vQNwjJ0x
此方法采用 JSON 字符串作为其参数。因此,如果您想使用字典,则需要将字典转换为请求正文的 json 字符串。
此外,您不需要设置 id、种类或 etag Google 创建这些值,因为您无法在请求中写入它们。 calendars resource
文档给出了如何进行调用的示例Calendar.insert
calendar = {
'summary': 'calendarSummary',
'timeZone': 'America/Los_Angeles'
}
created_calendar = service.calendars().insert(body=calendar).execute()
- 您正在向请求正文提供 JSON 格式的字符串,由 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 返回),您收到错误 Missing summary
。因此,您应该删除行 newCal = json.dumps(newCal)
.
kind
、id
和 etag
是不可写的属性(参见 Calendar resource representation);它们由 Google 提供,您设置的值将被忽略。因此,在您的字典中设置这些字段没有意义。
代码片段:
newCal = {
"summary": "Your summary",
"timeZone": "Europe/Rome"
}
res = service.calendars().insert(body=newCal).execute()
我正在尝试通过 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)
异常:
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 字典的摘要只是一个字符串(在本例中为“ARCHITETTURA”)
完整代码以备不时之需:https://pastebin.com/vQNwjJ0x
此方法采用 JSON 字符串作为其参数。因此,如果您想使用字典,则需要将字典转换为请求正文的 json 字符串。
此外,您不需要设置 id、种类或 etag Google 创建这些值,因为您无法在请求中写入它们。 calendars resource
文档给出了如何进行调用的示例Calendar.insert
calendar = {
'summary': 'calendarSummary',
'timeZone': 'America/Los_Angeles'
}
created_calendar = service.calendars().insert(body=calendar).execute()
- 您正在向请求正文提供 JSON 格式的字符串,由 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 返回),您收到错误Missing summary
。因此,您应该删除行newCal = json.dumps(newCal)
. kind
、id
和etag
是不可写的属性(参见 Calendar resource representation);它们由 Google 提供,您设置的值将被忽略。因此,在您的字典中设置这些字段没有意义。
代码片段:
newCal = {
"summary": "Your summary",
"timeZone": "Europe/Rome"
}
res = service.calendars().insert(body=newCal).execute()