【发布时间】:2021-01-05 19:25:39
【问题描述】:
由于 Microsoft 显然已对 Exchange Web 服务进行了 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 。最终我发现正在创建第二个配置对象;第一个按预期使用 OAUTH2 类型,第二个使用 NTLM 类型,这就是 create_oauth2_session 失败的那个。第二个实例带有我的凭据,但服务端点参数从 None 更改为 autodiscover.example.com/Autodiscover/Autodiscover.xml。作为自动发现过程的一部分(我认为),这是有道理的,但是为什么是第二个实例,为什么要更改身份验证类型?我难住了。非常感谢任何/所有帮助。
【问题讨论】:
标签: python oauth-2.0 exchangewebservices exchangelib