首页 > 解决方案 > 连接到 Exchange Web 服务时出现无效凭据错误

问题描述

我正在尝试使用 Python 中的 Exchangelib 库连接到 Exchange Web 服务以访问 Outlook 和 Teams 数据。我正在关注以下文章, https://github.com/ecederstrand/exchangelib/issues/747

下面是代码片段,

from exchangelib import IMPERSONATION, Account, Credentials, OAuth2Credentials, \
    Configuration, OAUTH2, Identity

client_id='XXX'
client_secret='XXX'
tenant_id='XXX'
user= 'XXX@YYY.onmicrosoft.com'

credentials = OAuth2Credentials(
    client_id=client_id,
    client_secret=client_secret,
    tenant_id=tenant_id,
    identity=Identity(primary_smtp_address=user)
)
config = Configuration(
    credentials=credentials,
    auth_type=OAUTH2,
    service_endpoint="https://outlook.office365.com/EWS/Exchange.asmx",
)
account = Account(
    user,
    config=config,
    autodiscover=False,
    access_type=IMPERSONATION,
)
print(account.root.all().count())

我仍然正确地提供了所有凭据,我收到一个无效的凭据错误,如下所示。

    886     if response.status_code == 401 and protocol.retry_policy.fail_fast:
    887         # This is a login failure
--> 888         raise UnauthorizedError('Invalid credentials for %s' % response.url)
    889     if 'TimeoutException' in response.headers:
    890         raise response.headers['TimeoutException']

UnauthorizedError: Invalid credentials for https://outlook.office365.com/EWS/Exchange.asmx

任何解决方案表示赞赏。提前致谢。

标签: python-3.xoauth-2.0exchange-serverexchangewebservicesexchangelib

解决方案


推荐阅读