当使用 google 日历 api for python 取消活动时,如何删除活动甚至活动系列?
How can I delete events or even event series when they are cancelled using google calendar api for python?
我创建了一个 python program/script 来帮助我从 google 表格文档中一次更新许多日历。它运行得非常整齐。
我还创建了一个函数,可以遍历所有感兴趣的日历并删除事件系列和类似的事件。此功能似乎只适用于部分日历,并非全部。当我查看我的在线 google 日历时,有些日历中的事件和事件系列仍未删除。当我打印事件系列 json 时,它显示状态为 'cancelled' 并且我无法对此做任何事情,只能转到它失败的每个日历并自己手动删除事件系列。
如有任何帮助,我们将不胜感激!!!!
我最近刚开始处理整个 google api 事情,所以我的代码中可能会遗漏一些东西。
def Delete_Events():
calendars=googleServices['Calendar'].calendarList().list().execute()
for calendar in calendars['items']:
if 'IAT' in calendar['summary'] or 'Possible' in calendar['summary']:
print(calendar['summary'])
gevents = googleServices['Calendar'].events().list(calendarId=calendar['id']).execute()
for gevent in gevents['items']:
try:
googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=gevent['id']).execute()
time.sleep(2)
pass
except Exception as e:
print(e)
instances = googleServices['Calendar'].events().instances(calendarId=calendar['id'],eventId=gevent['id']).execute()
for instance in instances['items']:
print(instance['summary'])
try:
googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=instance['recurringEventId']).execute()
time.sleep(2)
break
except Exception as e:
print(e)
break
这是我为活动打印的 json
{'kind': 'calendar#event', 'etag': '"3222549224124000"', 'id': 'm80coonp168fml4hec4tq777cc_20210101T130000Z', 'status': 'cancelled', 'recurringEventId': 'm80coonp168fml4hec4tq777cc', 'originalStartTime': {'dateTime': '2021-01-01T07:00:00-06:00', 'timeZone': 'America/Mexico_City'}}
这是实例json我可以打印出来
<HttpError 410 when requesting https://www.googleapis.com/calendar/v3/calendars/la2gtl3ih4r7o14aj7edhe1luc%40group.calendar.google.com/events/m80coonp168fml4hec4tq777cc_20210101T130000Z? returned "Resource has been deleted">
{'kind': 'calendar#event', 'etag': '"3222549224908000"', 'id': 'm80coonp168fml4hec4tq777cc_20210104T130000Z', 'status': 'cancelled', 'recurringEventId': 'm80coonp168fml4hec4tq777cc', 'originalStartTime': {'dateTime': '2021-01-04T07:00:00-06:00', 'timeZone': 'America/Mexico_City'}}
如您所见,return 相同的 json 信息和 410 错误提到 'Resource has been deleted' 这不是真的,因为我仍然可以查看、编辑并在网络浏览器上从我的 google 日历中手动删除它。
同样,任何帮助都会很棒!!
您正在迭代重复事件的实例,但总是试图删除重复事件而不是它的实例。看到这一行:
googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=instance['recurringEventId']).execute()
instance['recurringEventId']
对于每个实例都是相同的。你应该使用 instance['id']
我创建了一个 python program/script 来帮助我从 google 表格文档中一次更新许多日历。它运行得非常整齐。
我还创建了一个函数,可以遍历所有感兴趣的日历并删除事件系列和类似的事件。此功能似乎只适用于部分日历,并非全部。当我查看我的在线 google 日历时,有些日历中的事件和事件系列仍未删除。当我打印事件系列 json 时,它显示状态为 'cancelled' 并且我无法对此做任何事情,只能转到它失败的每个日历并自己手动删除事件系列。
如有任何帮助,我们将不胜感激!!!!
我最近刚开始处理整个 google api 事情,所以我的代码中可能会遗漏一些东西。
def Delete_Events():
calendars=googleServices['Calendar'].calendarList().list().execute()
for calendar in calendars['items']:
if 'IAT' in calendar['summary'] or 'Possible' in calendar['summary']:
print(calendar['summary'])
gevents = googleServices['Calendar'].events().list(calendarId=calendar['id']).execute()
for gevent in gevents['items']:
try:
googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=gevent['id']).execute()
time.sleep(2)
pass
except Exception as e:
print(e)
instances = googleServices['Calendar'].events().instances(calendarId=calendar['id'],eventId=gevent['id']).execute()
for instance in instances['items']:
print(instance['summary'])
try:
googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=instance['recurringEventId']).execute()
time.sleep(2)
break
except Exception as e:
print(e)
break
这是我为活动打印的 json
{'kind': 'calendar#event', 'etag': '"3222549224124000"', 'id': 'm80coonp168fml4hec4tq777cc_20210101T130000Z', 'status': 'cancelled', 'recurringEventId': 'm80coonp168fml4hec4tq777cc', 'originalStartTime': {'dateTime': '2021-01-01T07:00:00-06:00', 'timeZone': 'America/Mexico_City'}}
这是实例json我可以打印出来
<HttpError 410 when requesting https://www.googleapis.com/calendar/v3/calendars/la2gtl3ih4r7o14aj7edhe1luc%40group.calendar.google.com/events/m80coonp168fml4hec4tq777cc_20210101T130000Z? returned "Resource has been deleted">
{'kind': 'calendar#event', 'etag': '"3222549224908000"', 'id': 'm80coonp168fml4hec4tq777cc_20210104T130000Z', 'status': 'cancelled', 'recurringEventId': 'm80coonp168fml4hec4tq777cc', 'originalStartTime': {'dateTime': '2021-01-04T07:00:00-06:00', 'timeZone': 'America/Mexico_City'}}
如您所见,return 相同的 json 信息和 410 错误提到 'Resource has been deleted' 这不是真的,因为我仍然可以查看、编辑并在网络浏览器上从我的 google 日历中手动删除它。
同样,任何帮助都会很棒!!
您正在迭代重复事件的实例,但总是试图删除重复事件而不是它的实例。看到这一行:
googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=instance['recurringEventId']).execute()
instance['recurringEventId']
对于每个实例都是相同的。你应该使用 instance['id']