Python exchangelib OAuth2身份验证失败

问题描述

由于Microsoft显然已经对Exchange Web Services进行了EOL基本身份验证,所以我试图将我的Python应用程序转换为使用OAuth2。 Python 3.8,exchangelib 3.2.1,ms-exchange版本未知,但最近。相关代码如下:

from exchangelib import Credentials,Account,Message,Mailbox,ExtendedProperty,Configuration,\
                        OAuth2Credentials,OAuth2AuthorizationCodeCredentials,Identity,OAUTH2
from oauthlib.oauth2 import OAuth2Token

    credentials = OAuth2Credentials(client_id="xxxxxxxx-xxxx-etc",client_secret="xxxxxxxxxxxxx-etc",tenant_id="xxxxxxxx-xxxx-etc")
    config=Configuration(credentials=credentials,auth_type=OAUTH2)
    acct = Account(primary_smtp_address='testAddr@example.com',config=config,autodiscover=True)

结果:

ValueError: Auth type must be 'OAuth 2.0' for credentials type OAuth2Credentials
DEBUG:exchangelib.protocol:Server autodiscover.example.com: Closing sessions

在protocol.py中的BaseProtocol.create_oauth2_session()的开头引发异常:

        if self.auth_type != OAUTH2:
            raise ValueError('Auth type must be %r for credentials type OAuth2Credentials' % OAUTH2)

很简单……除了我在代码中明确指定了auth_type = OAUTH2之外。最终,我发现正在创建第二个Configuration对象。第一个具有预期的OAUTH2类型,第二个具有NTLM类型,这就是create_oauth2_session失败的原因。第二个实例带有我的凭据,但是服务端点参数从无更改为autodiscover.example.com/Autodiscover/Autodiscover.xml。作为自动发现过程的一部分,这很有意义(我认为),但是为什么要使用第二个实例,以及为什么要更改auth类型呢?我很沮丧任何/所有帮助都将不胜感激。

解决方法

这看起来像个错误。 This commit应该允许OAuth进行自动发现,但显然对您不起作用。我将调试器调试一下,找出auth_type值在哪里重置。我没有启用OAuth的EWS服务器,因此无法自己进行测试。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...