python - Python 连接到 Exchange /w Windows 身份验证
问题描述
我正在尝试在 Python 3.8脚本中创建一个脚本,该脚本可以连接到Exchange服务器并从邮箱中检索电子邮件。我正在尝试使用当前经过身份验证的用户对Exchange进行身份验证。如果我使用用户名和密码,我可以很好地进行身份验证。(尽量不存储任何密码或任何东西,只需使用当前经过身份验证的用户。)
我正在使用Python 3.8和Exchangelib连接Exchange,但无法弄清楚如何使用 Windows Auth(如果可能)。
任何帮助尝试实现这一点表示赞赏。
谢谢
我正在尝试做的示例:
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, OAuth2Credentials, \
OAuth2AuthorizationCodeCredentials, FaultTolerance, Configuration, NTLM, GSSAPI, SSPI, \
OAUTH2, Build, Version
from exchangelib.autodiscover import AutodiscoverProtocol
exchange_email = 'mailboxIWantToAccess@domain.com'
account = Account(exchange_email, autodiscover=True)
# account = Account(exchange_email, credentials=credentials, autodiscover=True)
account.root.refresh()
account.public_folders_root.refresh()
print(account.root.tree())
我得到的错误:
Traceback (most recent call last):
File "c:/Users/jk354/Documents/git.ourgitserver.com/client-info/script-ex.py", line 233, in <module>
account = Account(exchange_email, autodiscover=True)
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\account.py", line 85, in __init__
self.ad_response, self.protocol = discover(
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\autodiscover\discovery.py", line 23, in discover
return Autodiscovery(
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\autodiscover\discovery.py", line 88, in discover
ad_protocol = autodiscover_cache[cache_key]
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\autodiscover\cache.py", line 97, in __getitem__
protocol = AutodiscoverProtocol(config=Configuration(
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\protocol.py", line 73, in __init__
self._session_pool = self._create_session_pool()
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\protocol.py", line 160, in _create_session_pool
session_pool.put(self.create_session(), block=False)
File "C:\Users\jk354\AppData\Local\Programs\Python\Python38-32\lib\site-packages\exchangelib\protocol.py", line 233, in create_session
with self.credentials.lock:
AttributeError: 'NoneType' object has no attribute 'lock'
解决方案
我在 Windows/Exchangeserver 环境中使用 exchangelib,这是我的登录代码:
import getpass
from exchangelib import Configuration
from exchangelib import Credentials, Account
def login():
email = 'user@domain.com'
passwd = getpass.getpass(prompt='Password: ')
user_credentials = Credentials(email, passwd)
config = Configuration(server='exchangeserver',
credentials=user_credentials)
account = Account(primary_smtp_address=email, config=config,
credentials=user_credentials, autodiscover=False) #maybe try =True
return account
def main():
user_account = authenticate()
print(user_account.root.tree()) #printing the inbox
main()
input('Press enter to exit')
推荐阅读
- nginx - 使用 Gunicorn 和 NginX Gunicorn 部署 Flask 错误“失败,结果为 'exit-code'”
- python - 将两个不同数组中的元素插入与其列对应的新数组中
- html - CSS - 我不希望将“过渡”值设置为父级效果它的子级
- python - Python数组到mysql DB
- html - 将鼠标悬停在按钮上时,CSS top img 不直接位于背面 img 的顶部
- javascript - 如何使用 vanilla javascript 判断元素的顶部或底部何时滚动到页面顶部
- pandas - 如何使用一列的值作为字符串的一部分来填充另一列中的 NaN?
- typescript - 在函数中接受混合类型元组而不丢失类型检查
- python - 英文数字转整数(迭代函数)
- python - 如何使用 python playwright 读取 span 类的内容