【问题标题】:How can I delete events or even event series when they are cancelled using google calendar api for python?当使用 google calendar api for python 取消事件甚至事件系列时,如何删除它们?
【发布时间】:2021-01-24 16:58:47
【问题描述】:

我创建了一个 python 程序/脚本来帮助我从 google 表格文档中一次更新许多日历。它运行得非常整齐。

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

对此的任何帮助将不胜感激!!!!

我最近才开始使用整个 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'}}

如您所见,两者都返回相同的 json 信息以及 410 错误,提到“资源已被删除”,这是不正确的,因为我仍然可以从我的谷歌日历中手动查看、编辑和删除它网络浏览器。

再一次,任何帮助都会很棒!

【问题讨论】:

  • 你能举几个例子来说明会被删除和不会被删除的事件吗?它们的重复规则和日期将有利于繁殖

标签: python google-api google-calendar-api


【解决方案1】:

您正在迭代重复事件的实例,但始终试图删除重复事件而不是其实例。看到这一行:

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

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

【讨论】:

  • 您好,我已经尝试过这个解决方案,它确实删除了大部分事件,但是,当我在浏览器上看到我的实际日历应用程序时,很明显它没有删除所有事件。当我再次运行代码时,即使日历中仍有事件,它也没有得到任何事件......知道这里发生了什么吗?我很迷茫为什么它会这样
  • 我能够始终如一地删除所有事件的唯一方法是使用基本上相同的谷歌脚本......但是对于我们的实现,我们只能使用 python 和谷歌 api,这使得继续使用谷歌脚本删除事件无效。
  • 非常感谢您的帮助,这确实是我一直在努力使该项目完美运行的解决方案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多