【问题标题】:Python Connect To Exchange /w Windows AuthPython 连接到 Exchange /w Windows 身份验证
【发布时间】:2020-01-29 20:19:49
【问题描述】:

我正在尝试在 Python 3.8 脚本中创建一个脚本,该脚本可以连接到 Exchange 服务器并从邮箱中检索电子邮件。我正在尝试使用当前经过身份验证的用户通过 Exchange 进行身份验证。如果我使用用户名和密码,我可以很好地进行身份验证。 (尽量不存储任何密码或任何东西,只使用当前经过身份验证的用户。)

我正在使用 Python 3.8Exchangelib 连接 Exchange,但不知道如何使用 Windows Auth(如果可能)。 p>

感谢您尝试完成此操作的任何帮助。

谢谢

我正在尝试做的示例:

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'

https://github.com/ecederstrand/exchangelib

【问题讨论】:

  • Windows 身份验证是什么意思? NTLM? SSPI?克伯罗斯? exchangelib 不会自动检测您要使用的身份验证类型,因此您必须使用 Accountauth_type 参数显式指定它。见github.com/ecederstrand/exchangelib#setup-and-connecting
  • @ErikCederstrand 我不完全确定我会使用哪一个。在找不到任何我正在尝试做的事情的好例子之前,我从来没有做过这样的事情。我尝试设置 auth_type 但仍然获得相同的结果。

标签: python python-3.x windows-authentication exchangelib


【解决方案1】:

我在 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')

【讨论】:

  • 我刚刚更新了我的问题,因为我注意到它不是很清楚。我能够通过用户名和密码成功连接到交易所。但是,我想尝试不使用密码进行连接。我有一个我的帐户可以访问的邮箱,我希望能够使用当前经过身份验证的用户(即我)连接到 Exchange。
【解决方案2】:

说完就改用C#了。 我正在使用 EWS 托管 API。

https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications

我可以使用当前经过身份验证的用户通过此行连接到 Exchange:

service.UseDefaultCredentials = true;

【讨论】:

    【解决方案3】:

    终于为我工作了 kerberos 示例

    from requests_kerberos import HTTPKerberosAuth
    from exchangelib import DELEGATE, Account, Credentials, Configuration
    import exchangelib.autodiscover
    
    
    def auth_model(**kwargs):
        #get kerberos ticket 
        return HTTPKerberosAuth()
    
    
    def connect(server, email, username, password=1):
        from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
        # Care! Ignor Exchange self-signed SSL cert
        BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
    
        # fill Credential object with empty fields    
        creds = Credentials(
            username="",
            password=""
        )
    
        # add kerberos as GSSAPI auth_type
        exchangelib.transport.AUTH_TYPE_MAP["GSSAPI"] = auth_model
    
        # Create Config
        config = Configuration(server=server,credentials=creds, auth_type="GSSAPI")
        # return result
        return Account(primary_smtp_address=email, autodiscover=False, config = config, access_type=DELEGATE)
    
    def main():
        # Connection details
        server = 'mail.domain.ex'
        email = 'person@domain.ex'
        username = 'domain\person'
        account = connect(server, email, username)
    
        for item in account.inbox.all().order_by('-datetime_received')[:100]:
            print(item.subject, item.sender, item.datetime_received)
    
    main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 2013-05-07
      相关资源
      最近更新 更多