You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MS Graph API读取Exchange日历无法获取Recurring Events的问题求助

MS Graph API读取Exchange日历无法获取Recurring Events的问题求助

大家好,我现在遇到一个使用MS Graph API读取Exchange日历的问题:非重复事件都能正常获取,但重复(Recurring)事件完全拿不到。

先说明下前提:

  • OAuth2认证是成功的,这部分代码没问题就不贴了
  • 测试时传入的时间参数是:
    • Filter_Start_Date = "2024-12-11"
    • Filter_End_Date = "2024-12-11"

我的代码如下:

# Update filters)
Filter_Start_Date = Filter_Start_Date + "T00:00:00Z"
Filter_End_Date = Filter_End_Date + "T00:00:00Z"

headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json",
    "Prefer": 'outlook.timezone="Europe/Paris"'}

params = {
    "$filter": f"start/dateTime ge '{Filter_Start_Date}' and end/dateTime le '{Filter_End_Date}'",  #! 怀疑问题出在这里的过滤逻辑!!!!
    "$orderby": f"start/dateTime desc",
    "$top": 1000,
    "$count": "true",
    "$select": "subject, start, end, categories, recurrence, showAs, location, isAllDay, bodyPreview"}

Events_downloaded = {}
events_url = f"https://graph.microsoft.com/v1.0/users/{username}/events"
events_response = requests.get(url=events_url, headers=headers, params=params)

Counter = 0 
if response.status_code == 200:
    # Init page 
    Events = events_response.json()

有没有大佬能帮我看看哪里出错了?谢谢!


我来帮你分析下问题出在哪,以及怎么解决:

为什么拿不到重复事件?

你现在的过滤条件有两个坑:

  1. 过滤逻辑只针对重复事件的主记录:MS Graph里的重复事件会有一个“主事件”,它的start/dateTime是这个重复系列的第一个实例时间,比如如果你的重复事件是从10月份开始的,那主事件的起始时间早于你设置的12月11日,自然会被过滤掉。
  2. 结束时间设置错误:你把Filter_End_Date拼成了2024-12-11T00:00:00Z,这个时间是12月11日的零点,而当天的事件结束时间肯定在零点之后,所以end/dateTime le '2024-12-11T00:00:00Z'会把所有当天的事件都排除大半,包括重复事件。

怎么解决?

推荐两种方案,其中第二种更简单直接:

方案一:用occursin函数+修正结束时间

MS Graph提供了专门的occursin函数,用来判断事件是否在指定时间范围内发生(包括重复事件的实例),同时把结束时间改成目标日期的次日零点,这样才能覆盖全天:

# 先修正结束时间:改成目标日期的下一天,这样才能包含12月11日全天的事件
Filter_Start_Date = Filter_Start_Date + "T00:00:00Z"
Filter_End_Date = "2024-12-12T00:00:00Z"  # 原日期是2024-12-11,这里加一天

params = {
    "$filter": f"occursin('{Filter_Start_Date}', '{Filter_End_Date}')",
    "$orderby": "start/dateTime desc",
    "$top": 1000,
    "$count": "true",
    "$select": "subject, start, end, categories, recurrence, showAs, location, isAllDay, bodyPreview",
    "$expand": "occurrences"  # 这个参数用来展开重复事件的具体实例详情
}

方案二:用calendarView端点(更推荐)

MS Graph专门有个calendarView端点,就是用来获取指定时间范围内的日历视图的,它会自动处理重复事件,直接返回该时间段内的所有事件实例(包括重复的每个 occurrence),用法也更简单:

# 同样修正结束时间为次日零点
Filter_Start_Date = Filter_Start_Date + "T00:00:00Z"
Filter_End_Date = "2024-12-12T00:00:00Z"

headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json",
    "Prefer": 'outlook.timezone="Europe/Paris"'}

params = {
    "$orderby": "start/dateTime desc",
    "$top": 1000,
    "$select": "subject, start, end, categories, recurrence, showAs, location, isAllDay, bodyPreview"}

# 注意这里的URL换成了calendarView,并且把时间参数直接放在URL里
events_url = f"https://graph.microsoft.com/v1.0/users/{username}/calendar/calendarView?startDateTime={Filter_Start_Date}&endDateTime={Filter_End_Date}"
events_response = requests.get(url=events_url, headers=headers, params=params)

最后再提醒两个小细节

  • 用calendarView的时候,不需要在$filter里写时间条件,直接把startDateTime和endDateTime挂在URL上就行,这是这个端点的要求。
  • 确保你的access_token有Calendars.Read权限,没有的话是读不到日历的哦。

备注:内容来源于stack exchange,提问作者Jan Vaško

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 17:39:30