Google 日历 API:删除事件不会删除事件 ID
Google Calendar API: Deleting a Event does not delete an Event Id
首先:我创建了一个新事件:
event = {
"id": "tph13",
"start": {
"dateTime": convert_to_RFC_datetime(2021,5,10,8,15),
"timeZone": "Europe/Zurich"
},
"end": {
"dateTime": convert_to_RFC_datetime(2021,5,10,12,00),
"timeZone": "Europe/Zurich"
},
"summary": "TEST_EVENT"
}
event = service.events().insert(calendarId='primary', body=event).execute()
这成功地在 Google 日历中创建了一个事件!
然后:我再次删除事件:
service.events().delete(calendarId='primary', eventId='tph13').execute()
成功删除事件(至少我再也看不到了)。
然后:我重新创建相同的事件:
event = {
"id": "tph13",
"start": {
"dateTime": convert_to_RFC_datetime(2021,5,10,8,15),
"timeZone": "Europe/Zurich"
},
"end": {
"dateTime": convert_to_RFC_datetime(2021,5,10,12,00),
"timeZone": "Europe/Zurich"
},
"summary": "TEST_EVENT"
}
event = service.events().insert(calendarId='primary', body=event).execute()
...但随后出现此错误:
'The requested identifier already exists.'
我做错了什么?
(对于那些感兴趣的人)
def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
return dt
要么是传播问题,要么这些 ID 永远不能再使用。
来自文档:
Due to the globally distributed nature of the system, we cannot guarantee that ID collisions will be detected at event creation time.
https://developers.google.com/calendar/v3/reference/events/insert
虽然这没有解决您的确切问题,但它指出了一个重要问题,即更新需要时间来传播。
虽然不允许创建以前删除的 ID 有一些优势(如下所述),但有可能最终这些 ID 再次可用。如果真实 ID 被删除,可能需要几天时间才能在系统中传播。这是假设它们已从系统中删除。十有八九,ID 会长期存在。
为什么要保留 ID?
一个或多个其他资源可能指向旧 ID。如果一个应用程序依赖于一个特定的事件来收集一些信息,然后发现这个 ID 有完全不同的信息,那就有点奇怪了。对于其他资源来说,发现该事件已被删除比找到一个完全不同的事件更好。
事实上,当您对已删除的事件发出 GET
请求时,它实际上 return 具有一个重要字段的事件:
"status": "cancelled",
这是有道理的,因为可能很多人都在他们的日历中添加了此事件 ID 的提醒,如果 API 允许您完全更改最近删除的事件,那么人们可能会开始随机收到提醒事件。
它显示为“已取消”的事实表明,这些 ID 实际上可能永远不会被删除,或者至少保留足够长的时间以使上述问题永远不会成为问题。
更改事件
如果您想保留相同的 ID,但又想完全更新事件,那么您将需要使用 update
or patch
端点。
参考
首先:我创建了一个新事件:
event = {
"id": "tph13",
"start": {
"dateTime": convert_to_RFC_datetime(2021,5,10,8,15),
"timeZone": "Europe/Zurich"
},
"end": {
"dateTime": convert_to_RFC_datetime(2021,5,10,12,00),
"timeZone": "Europe/Zurich"
},
"summary": "TEST_EVENT"
}
event = service.events().insert(calendarId='primary', body=event).execute()
这成功地在 Google 日历中创建了一个事件!
然后:我再次删除事件:
service.events().delete(calendarId='primary', eventId='tph13').execute()
成功删除事件(至少我再也看不到了)。
然后:我重新创建相同的事件:
event = {
"id": "tph13",
"start": {
"dateTime": convert_to_RFC_datetime(2021,5,10,8,15),
"timeZone": "Europe/Zurich"
},
"end": {
"dateTime": convert_to_RFC_datetime(2021,5,10,12,00),
"timeZone": "Europe/Zurich"
},
"summary": "TEST_EVENT"
}
event = service.events().insert(calendarId='primary', body=event).execute()
...但随后出现此错误:
'The requested identifier already exists.'
我做错了什么?
(对于那些感兴趣的人)
def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
return dt
要么是传播问题,要么这些 ID 永远不能再使用。
来自文档:
Due to the globally distributed nature of the system, we cannot guarantee that ID collisions will be detected at event creation time.
https://developers.google.com/calendar/v3/reference/events/insert
虽然这没有解决您的确切问题,但它指出了一个重要问题,即更新需要时间来传播。
虽然不允许创建以前删除的 ID 有一些优势(如下所述),但有可能最终这些 ID 再次可用。如果真实 ID 被删除,可能需要几天时间才能在系统中传播。这是假设它们已从系统中删除。十有八九,ID 会长期存在。
为什么要保留 ID?
一个或多个其他资源可能指向旧 ID。如果一个应用程序依赖于一个特定的事件来收集一些信息,然后发现这个 ID 有完全不同的信息,那就有点奇怪了。对于其他资源来说,发现该事件已被删除比找到一个完全不同的事件更好。
事实上,当您对已删除的事件发出 GET
请求时,它实际上 return 具有一个重要字段的事件:
"status": "cancelled",
这是有道理的,因为可能很多人都在他们的日历中添加了此事件 ID 的提醒,如果 API 允许您完全更改最近删除的事件,那么人们可能会开始随机收到提醒事件。
它显示为“已取消”的事实表明,这些 ID 实际上可能永远不会被删除,或者至少保留足够长的时间以使上述问题永远不会成为问题。
更改事件
如果您想保留相同的 ID,但又想完全更新事件,那么您将需要使用 update
or patch
端点。