问题描述
我正在尝试通过我的作品代理使用 Pop3 协议访问一些邮箱。我可以获得我的 oauth 令牌,并使用 Chilkat2 HTTP 库访问我的信息。
http = chilkat2.Http()
http.AuthToken = accesstoken
http.ProxyDomain = "Some Proxy"
http.ProxyPort = Some Port
answer = http.QuickGetStr("https://graph.microsoft.com/v1.0/me")
print(answer)
返回这个
{"@odata.context":"https://graph.microsoft.com/v1.0/$Metadata#users/$entity"................等
这会从 Windows 服务器返回我的个人资料详细信息,因此我知道我的 oauth2 令牌有效且有效。
然后我尝试使用 MailMan 协议打开我的 POP3 邮箱我遇到了身份验证错误,我运行下面的代码
mailman = chilkat2.MailMan()
mailman.HttpProxyHostname = "Some Proxy"
mailman.HttpProxyPort = Some Port
mailman.MailHost = "outlook.office365.com"
mailman.MailPort = 995
mailman.PopSsl = True
mailman.PopUsername = username
mailman.PopPassword = ""
mailman.OAuth2Accesstoken = accesstoken
mailman.Pop3EndSession() #close session as program keeps breaking and leaving session open
success = mailman.Pop3Connect()
if (success != True):
print(mailman.LastErrorText)
sys.exit()
# Authenticate..
success = mailman.Pop3Authenticate()
if (success != True):
print(mailman.LastErrorText)
sys.exit()
然而,authenticate 命令总是返回 false,Chilkat 错误日志显示如下:
ChilkatLog:
Pop3Authenticate:
DllDate: Feb 9 2021
ChilkatVersion: 9.5.0.86
UnlockPrefix: Auto unlock for 30-day trial
Architecture: Little Endian; 64-bit
Language: Python 3.9.4 (tags/v3.9.4:1f2e308,Apr 6 2021,13:40:21) [MSC v.1928 64 bit (AMD64)],win32
VerboseLogging: 0
Pop3Authenticate:
username: my username
popSPA: 0
greeting: +OK The Microsoft Exchange POP3 service is ready. [TABPADIAUAAyADYANQBDAEEAMAAzADgANgAuAEcAQgBSAFAAMgA2ADUALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
pop_office365_xoauth2:
PopCmdsent: AUTH XOAUTH2
PopCmdResp: +
auth_xoauth2_response_1: +
PopCmdsent: <base64 string in XOAUTH2 format>
PopCmdResp: -ERR Authentication failure: unkNown user name or bad password.
POP3 response indicates failure.
AUTH_XOAUTH2_response: -ERR Authentication failure: unkNown user name or bad password.
--pop_office365_xoauth2
POP3 authentication Failed
--Pop3Authenticate
Failed.
--Pop3Authenticate
--ChilkatLog
我不知所措,我已经使用了所有应该允许我使用令牌进行访问的范围组合,但是我无法让它与 Chilkat 中的任何电子邮件库一起使用;似乎与服务器连接良好,但总是无法通过身份验证。有人对此有任何想法吗?
解决方法
我的猜测是在 Azure 中设置不正确,和/或您在获取 OAuth2 访问令牌时没有要求正确的范围。 此示例包含描述 Azure Active Directory 中的应用程序注册的注释:https://www.example-code.com/chilkat2-python/office365_oauth2_access_token.asp 在每一步,都有一个指向 Azure 控制台屏幕的链接,就像我在做的那样。
此外,您的范围应如下所示:
oauth2.Scope = "openid profile offline_access https://outlook.office365.com/SMTP.Send https://outlook.office365.com/POP.AccessAsUser.All https://outlook.office365.com/IMAP.AccessAsUser.All"
Microsoft 文档中存在范围不同(或旧?)的地方。 交互式 3-legged OAuth2 流程只需执行一次。拥有 OAuth2 访问令牌后,无需用户交互即可持续刷新 如下所示:https://www.example-code.com/chilkat2-python/office365_refresh_access_token.asp
或者,您可以尝试资源所有者授权流程,如下所示: https://www.example-code.com/chilkat2-python/office365_resource_owner_password_credentials_grant.asp 资源所有者流程是非交互式的,适用于访问其自己的 O365 帐户的应用。
如果有帮助,请告诉我。