使用 Python 访问 Outlook API

Cer*_*rin 2 python outlook soap

如何使用 Python 访问 XML API 以在 Microsoft Office 365 帐户中创建日历事件?

我在http://outlook.office365.com上有一个托管帐户,我正在尝试使用 Python 库https://github.com/linkedin/pyexchange(几乎逐字使用示例代码)来创建日历事件,但 Outlook 拒绝我的凭据并显示错误“无法连接到 Exchange:HTTP 错误 401:未经授权”

我正在使用端点https://outlook.office365.com/EWS/Exchange.asmx,以及我通常用来登录 Web UI 的用户名和密码。

我是否需要专门设置或配置我的帐户才能从 API 访问它?

小智 5

您的 Outlook 可能不再使用 NTLM 身份验证。(来源:这里这里这里

在我们的帐户从本地迁移到 O365 后,我遇到了同样的问题,并且不得不使用另一个库来访问 Exchange 上的日历。我在这里使用了 O365 库:https ://pypi.org/project/O365

按照文档在 Azure 中创建注册并验证您的应用程序,然后您应该能够像以前一样使用以下示例代码访问您的日历:

from O365 import Account
from datetime import datetime
credentials = ('your_client_id', 'client_secret')

scopes = ['https://outlook.office365.com/Calendars.Read']
account = Account(credentials)

schedule = account.schedule()
calendar = schedule.get_default_calendar()

q = calendar.new_query('start').greater_equal(datetime(2019, 5, 20))
q.chain('and').on_attribute('end').less_equal(datetime(2019, 10, 24))

events = calendar.get_events(query=q, include_recurring=True)

for event in events:
    print(event.subject)
Run Code Online (Sandbox Code Playgroud)