使用 Google Calendar API 更新所有未来事件

问题描述 投票:0回答:1

我希望能够使用 Google Calendar API 对事件进行修改并将其应用于将来的后续事件。

我尝试修补和更新,甚至使用自己的 recurringEventId 来制作补丁,但似乎没有任何效果。我目前正在使用 Python googleapiclient 来完成这项工作,这是否可以从 API 方面实现?因为这在 UI 中绝对是可能的。

{'kind': 'calendar#event',
 'etag': 'etag',
 'id': '<recurr>_<time>',
 'status': 'confirmed',
 'htmlLink': 'https://www.google.com/calendar/event',
 'created': '2024-07-11T01:28:33.000Z',
 'updated': '2024-07-11T01:32:53.501Z',
 'summary': 'Test',
 'description': 'Content Here',
 'creator': {'email': 'kevin'},
 'organizer': {'email': 'calendar.google.com',
  'displayName': 'Pipeline Scheduler - Staging',
  'self': True},
  'start': {'dateTime': '2024-07-10T18:30:00-07:00',
   'timeZone': 'America/Los_Angeles'},
  'end': {'dateTime': '2024-07-10T19:30:00-07:00',
  'timeZone': 'America/Los_Angeles'},
 'recurringEventId': '<recurr>',
 'originalStartTime': {'dateTime': '2024-07-10T18:30:00-07:00',
  'timeZone': 'America/Los_Angeles'},
 'iCalUID': '<recurr>@google.com',
 'sequence': 3,
  'reminders': {'useDefault': True},
  'eventType': 'default'}

对于这样的事件,我只能使用以下命令修补或更新单个事件(不进行批处理,因为对于我们的用例而言,它花费的时间太长):

以下是我们查询事件的方法: service.events().patch(calendarId=calendarId, eventId=event['id'], body=event).execute()

now = datetime.utcnow()
start = now.isoformat() + "Z"
end = datetime.combine(now.date(), time.max).isoformat() + "Z"

try:
    events_result = (
        service.events()
        .list(
            calendarId=calendar_id,
            timeMin=start,
            timeMax=end,
            singleEvents=True,
            orderBy="startTime"
        )
        .execute()
    )
    events = events_result.get("items", [])

    if not events:
        log.info("No events found today (UTC)")

    return events

except HttpError as e:
    log.error(f"Error fetching events: {e}")
    return []
python google-calendar-api
1个回答
0
投票

您将获得重复事件的单个实例 (

singleEvents=True
)。您需要获取“主要”重复事件来更新所有实例。您有两个选择:

  1. 使用
    singleEvents=False
  2. 从一开始就获取“主要”重复事件
  3. 当您有重复事件的实例时,获取它的
    recurringEventId
    并获取“主要”事件:
event_instance = ...

event = service.events()
    .get(
        calendarId=calendar_id,
        eventId=event_instance['recurringEventId']
    )
    .execute()
)

# update the event
...

service.events().update(
    calendarId=calendar_id,
    eventId=event['id'],
    body=event
)
© www.soinside.com 2019 - 2024. All rights reserved.