我有一个 Python 脚本,可以读取 Office 365 电子邮件并下载附件。 我正在使用 Exchangelib 并尝试从基本身份验证切换到 OAUTH2。
我的旧脚本进行身份验证时没有任何问题:
from exchangelib import Credentials, Account
username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="344c4c4c744d4d4d1a575b59" rel="noreferrer noopener nofollow">[email protected]</a>'
pwd = '123456'
credentials = Credentials(username, pwd)
account = Account(username,
credentials=credentials, autodiscover=True)
新的一个:
from exchangelib import OAuth2Credentials
username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="720a0a0a320b0b0b5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>'
tenant_id = 'TENANT ID'
client_id = 'CLIENT ID'
secret_value = 'CLIENT SECRET'
credentials = OAuth2Credentials(client_id=client_id, client_secret=secret_value, tenant_id=tenant_id)
account = Account(username,
credentials=credentials, autodiscover=True)
新脚本返回错误:
exchangelib.errors.ErrorNonExistentMailbox: The SMTP address has no mailbox associated with it
我已在 Azure 门户中创建并注册了应用程序。电子邮件/用户已添加到应用程序。
最佳答案
我尝试在我的环境中重现相同的结果并得到以下结果:
我创建了一个 Azure AD 应用程序,并在管理员同意的情况下添加了 API 权限,如下所示:
现在,我运行了与您相同的代码,并得到了相同的错误,如下所示:
from exchangelib import OAuth2Credentials
username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="572f2f2f172332393639237938393a3e342538243831237934383a" rel="noreferrer noopener nofollow">[email protected]</a>'
tenant_id = 'TENANT ID'
client_id = 'CLIENT ID'
secret_value = 'CLIENT SECRET'
credentials = OAuth2Credentials(client_id=client_id, client_secret=secret_value, tenant_id=tenant_id)
account = Account(username, credentials=credentials, autodiscover=True)
回应:
要解决该错误,请尝试对代码进行一些更改,如下所示:
from exchangelib import OAuth2Credentials
username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98e0e0e0d8ecfdf6f9f6ecb6f7f6f5f1fbeaf7ebf7feecb6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>' ## make sure to give valid address
tenant_id = 'TENANT ID'
client_id = 'CLIENT ID'
secret_value = 'CLIENT SECRET'
version = Version(build=Build(15, 0, 12, 34))
credentials = OAuth2Credentials(client_id=client_id, client_secret=secret_value, tenant_id=tenant_id)
config = Configuration(service_endpoint = 'https://outlook.office365.com/EWS/Exchange.asmx',
credentials=credentials,
auth_type=OAUTH2 ,
version=version)
account = Account(username, credentials=credentials, autodiscover=False, config=config, access_type=IMPERSONATION)
mails = list(account.inbox.filter(is_read=False).only(
'is_read', 'subject', 'body','text_body','datetime_received',
'sender','to_recipients','cc_recipients','bcc_recipients',
'attachments','importance'
).order_by('datetime_received')[:1])
print(mails)
回应:
确保用户是否已分配所需的许可证,如下所示:
转到 Azure Active Directory -> 用户 -> 所有用户 -> 选择用户 -> 许可证
关于python - Exchangelib OAUTH 身份验证 NonExistentMailbox 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74127098/