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

huangapple go评论79阅读模式
英文:

Not able to login IMP4 outlook email with azure resgistration

问题

import imaplib
import msal

# IMAP筛选条件
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

# 创建IMAP连接
mail = imaplib.IMAP4_SSL(email_host, port)

# 配置信息
conf = {
    "authority": "https://login.microsoftonline.com/xxxxxxxxxxxxxxx",
    "client_id": "xxxxxxxxxxxxxxxxxx",  # 应用程序ID
    "scope": ['https://outlook.office365.com/.default'],
    "secret": "xxxxxxxxxxxxxxxxxxxxxxxxx",  # 密钥-值
    "secret-id": "xxxxxxxxxxxxxxxxxxxxxx",  # 密钥-ID
}

# 生成身份验证字符串函数
def generate_auth_string(user, token):
    return f"user={user}\x01auth=Bearer {token}\x01\x01"

# 创建ConfidentialClientApplication
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"))

# 创建IMAP连接
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"))

这是您提供的代码的中文翻译部分,不包括注释和问题描述。

英文:
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"))

This code was working fine from last one year. suddenly it stop working.
credentials all ok. successfully able to generate token.

The error I am getting is
after line mail.starttls() at this line it goes to stuck.
Error 12:08.14 > b'EMFF1 STARTTLS'
TSL version I am using is TLS 1.2

As ref https://stackoverflow.com/questions/73902642/office-365-imap-authentication-via-oauth2-and-python-msal-library
still not success.

答案1

得分: 1

也许Microsoft暂时中断了starttls命令。有一个替代方法可以使用,从一开始就使用TLS,而不是升级纯文本连接:

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

注意,您可能应该使用主机名outlook.office365.com参考)。SMTP用于发送邮件,未来可能不会指向与IMAP服务器托管在同一主机上的主机。此外,似乎也更喜欢在端口993上使用完整的TLS。

英文:

Perhaps Microsoft has temporarily broken the starttls command. There's an alternative you can use which does TLS from the start, rather than upgrading a plaintext connection:

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

Note, you should probably also be using the hostname outlook.office365.com (ref). SMTP is for outgoing mail and may not, in the future, point to the same host as the IMAP servers are hosted on. It also appears full TLS on port 993 is preferred as well.

huangapple
  • 本文由 发表于 2023年7月17日 16:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定