由于微软显然已经停止了Exchange Web服务的基本身份验证,我正在尝试将我的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,这是一个简单的enough...except。最后,我发现正在创建第二个Configuration对象;第一个对象的类型为OAUTH2,第二个对象的类型为NTLM,这就是create_oauth2_session失败的原因。第二个实例包含我的凭据,但服务端点参数从None更改为autodiscover.example.com/Autodiscover/Autodiscover.xml.这作为自动发现过程的一部分是有意义的(我认为),但是为什么是第二个实例,为什么要更改auth类型??我被难住了。任何/所有的帮助都非常感谢。
发布于 2020-09-19 21:44:04
这看起来像是个bug。This commit应该允许OAuth自动发现,但它显然不适合你。我会打开一个调试器,找出auth_type值被重置的位置。我没有启用OAuth的EWS服务器,所以我不能自己测试。
https://stackoverflow.com/questions/63962810
复制相似问题