You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启用exchangelib DEBUG日志后无法找到输出位置的技术求助

Troubleshooting exchangelib "Failed to get auth type from service" and Logging Issues

Fixing Exchangelib Debug Logging

Just setting the log level for exchangelib isn't enough—Python's logging system needs explicit handlers to output logs to console or files. By default, the logger may not have any handlers attached, especially in pytest environments where logging is often captured or configured differently.

Here's how to properly configure logging to output both to console and a file in your test directory:

import logging
from exchangelib import Credentials, Account, Configuration

# Configure exchangelib logger
logger = logging.getLogger('exchangelib')
logger.setLevel(logging.DEBUG)
logger.propagate = False  # Prevent logs from being swallowed by pytest's root logger

# Create file handler for debug logs
file_handler = logging.FileHandler('./exchangelib_debug.log')
file_handler.setLevel(logging.DEBUG)

# Create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)

# Set formatted log messages
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)

# Attach handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)

If you don't see console logs when running pytest, use the -s flag to disable stdout/stderr capture:

pytest -s your_test_file.py

Resolving "Failed to get auth type from service" Error

Once logging works, you'll get detailed logs about the auth negotiation process. Common causes for this error include:

  • Autodiscover failures: Local Exchange servers often don't support autodiscover reliably. Disable it and use explicit server configuration.
  • Missing auth type setup: Local Exchange typically uses NTLM auth, which exchangelib may not auto-detect. Force NTLM explicitly.
  • Incorrect server URL: Ensure you're using the full EWS endpoint (e.g., https://your-exchange-server/EWS/Exchange.asmx).
  • Missing dependencies: For NTLM auth, install required packages: pip install requests-ntlm pyspnego
  • Network restrictions: Verify the EWS endpoint is reachable from your test environment (firewalls/proxies may block access).

Example of explicit NTLM configuration for local Exchange:

creds = Credentials(username='YOUR_DOMAIN\\your_username', password='your_password')
config = Configuration(
    server='your-exchange-server.domain.local',
    credentials=creds,
    auth_type='ntlm',
    # Add this only if your server uses self-signed SSL certs (not recommended for production)
    # verify=False
)
account = Account(
    primary_smtp_address='your.email@domain.local',
    config=config,
    autodiscover=False
)

# Test the connection
account.root.refresh()

Before running, confirm the EWS endpoint is accessible: navigate to https://your-exchange-server/EWS/Exchange.asmx in a browser—you should see an XML response or an authentication prompt.

内容的提问来源于stack exchange,提问作者Jon Champlin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 08:24:56