无法使用Azure重新注册登录IMP4 Outlook电子邮件

4c8rllxm  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(88)
import imaplib
import msal

imap_filter = '(FROM "xxxxxxxxxxxx@maybank.com" SINCE "SINCE 13-Jul-2023" BEFORE "BEFORE 17-Jul-2023")'

email_user = 'xxxxxxxxxx@aarna.capital'
email_host = 'smtp.office365.com'
port = 993

mail = imaplib.IMAP4_SSL(email_host,port)

conf = {
            "authority": "https://login.microsoftonline.com/xxxxxxxxxxxxxxx",
            "client_id": "xxxxxxxxxxxxxxxxxx", #AppID
            "scope": ['https://outlook.office365.com/.default'],
            "secret": "xxxxxxxxxxxxxxxxxxxxxxxxx", #Key-Value
            "secret-id": "xxxxxxxxxxxxxxxxxxxxxx", #Key-ID
        }

def generate_auth_string(user, token):
    return f"user={user}\x01auth=Bearer {token}\x01\x01"    

app = msal.ConfidentialClientApplication(conf['client_id'], authority=conf['authority'],
                                         client_credential=conf['secret'])

result = app.acquire_token_silent(conf['scope'], account=None)

if not result:
    print("No suitable token in cache.  Get new one.")
    result = app.acquire_token_for_client(scopes=conf['scope'])
    print(result)
if "access_token" in result:
    print(result['token_type'])
    print(result)
else:
    print(result.get("error"))
    print(result.get("error_description"))
    print(result.get("correlation_id"))

mail = imaplib.IMAP4('smtp.office365.com')
mail.debug = 4
mail.starttls()
mail.authenticate("XOAUTH2", lambda x: generate_auth_string(email_user, result['access_token']).encode("utf-8"))

字符串
这段代码从去年开始运行良好。它突然停止工作。证件都OK。成功生成令牌。
我得到的错误是行mail.starttls()后,在这一行它去卡住。错误12:08.14 > b'EMFF1 STARTTLS'我使用的TSL版本是TLS 1.2
参考Office 365 IMAP authentication via OAuth2 and python MSAL library仍然不成功。

yhxst69z

yhxst69z1#

也许微软暂时中断了starttls命令。有一种替代方案可以从一开始就使用TLS,而不是升级明文连接:

mail = imaplib.IMAP4_SSL('smtp.office365.com')
mail.debug = 4
...

字符串
注意,您可能还应该使用主机名outlook.office365.comref)。SMTP用于发送邮件,将来可能不会指向托管IMAP服务器的同一主机。端口993上的完全TLS似乎也是首选。

相关问题