使用google日历api for python取消事件或事件系列时,如何删除它们?

2024-09-27 22:26:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个python程序/脚本来帮助我从GoogleSheets文档一次更新多个日历。它跑得很整齐

我还创建了一个函数,可以遍历所有感兴趣的日历,并删除事件系列和事件。此功能似乎仅适用于某些日历,而不是所有日历。当我查看我的在线谷歌日历时,有些日历仍然没有删除事件和事件系列。当我打印出事件系列json时,它显示状态为“已取消”,我对此无能为力,除了转到它失败的每个日历并手动删除事件系列之外

在此方面的任何帮助都将不胜感激

我最近刚刚开始使用整个GoogleAPI,所以我的代码中可能遗漏了一些东西

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'}}

正如您所看到的,两者都返回了相同的json信息,并且还出现了410错误,指出“资源已被删除”,这是不正确的,因为我仍然能够在web浏览器上从我的google日历中手动查看、编辑和删除它

再说一次,任何帮助都将是惊人的


Tags: inidjsonforexecute事件itemssummary
1条回答
网友
1楼 · 发布于 2024-09-27 22:26:30

您正在迭代周期性事件的实例,但始终尝试删除周期性事件而不是其实例。请看这一行:

googleServices['Calendar'].events().delete(calendarId=calendar['id'], eventId=instance['recurringEventId']).execute()

instance['recurringEventId']对于每个实例都是相同的。您应该使用instance['id']

相关问题 更多 >

    热门问题