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

如何通过O365和Microsoft Graph API访问共享日历事件?

问题原因与解决方案

核心问题:Resource参数不支持非微软系邮箱

你用name@gmail.com作为resource参数报错,是因为Microsoft Graph API的/users/{id}端点仅支持微软生态内的邮箱账户(比如Microsoft 365组织邮箱、outlook.com/hotmail.com个人账户),Gmail不属于这个范围,所以会返回"用户无效"的404错误。

针对不同场景的解决方案

场景1:共享日历属于微软系邮箱(如xxx@outlook.com、xxx@company.com)

如果共享日历的所有者是微软生态内的账户,可按以下方式调整代码:

# 替换为共享日历所有者的微软系邮箱
schedule2 = account.schedule(resource='xxx@outlook.com')
# 获取所有者的默认日历(如果共享的是默认日历)
calendar2 = schedule2.get_default_calendar()

# 如果共享的是自定义日历,需先列出所有日历再筛选
all_calendars = schedule2.get_calendars()
for cal in all_calendars:
    print(f"日历名称:{cal.name},ID:{cal.id}")
# 根据名称定位目标共享日历
target_cal = next(cal for cal in all_calendars if cal.name == "自定义共享日历名称")

注意事项:

  • 确认应用已配置Calendars.Read.Shared的应用权限(而非委托权限),且已获得管理员同意
  • 确保日历所有者已将日历共享给你的应用对应的Azure AD服务主体,或组织内有允许跨用户访问日历的权限策略

场景2:共享日历属于Gmail账户

如果是Gmail用户将日历共享给你的Outlook账户,无法通过resource参数直接访问,需先在你的Outlook中添加该共享日历,再通过自身的Schedule对象获取:

schedule = account.schedule()
# 获取当前账户下的所有日历(包括已添加的共享Gmail日历)
all_calendars = schedule.get_calendars()
# 遍历找到目标共享日历
for cal in all_calendars:
    if "Gmail共享日历" in cal.name:  # 替换为实际日历名称
        target_cal = cal
        break
# 获取该日历的事件
events = target_cal.get_events()
for event in events:
    print(f"事件标题:{event.subject},时间:{event.start}")

内容的提问来源于stack exchange,提问作者Jess

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:47:18