使用 OAuth2.0 在线 Office365 面临连接问题
我已经设置了应用程序权限以及 IMAP 和 SMTP 连接。基本身份验证似乎工作正常。我相信 IMAP 已启用。我的应用程序配置为任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户,并使用授予类型授权代码。
和委派的 Microsoft Graph 范围https://graph.microsoft.com/IMAP.AccessAsUser.All https://graph.microsoft.com/IMAP.AccessAsUser.All已添加:添加了客户端范围 https://i.stack.imgur.com/tcwr2.png
请求的授权码为访问令牌请求https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token https://login.microsoftonline.com/%7Btenant%7D/oauth2/v2.0/token请求的访问令牌,资源为“https://graph.microsoft.com”
请求访问令牌图像 https://i.stack.imgur.com/Vbyky.png
已成功接收范围为 IMAP.AccessAsUser.All SMTP.Send 的访问令牌
{
"token_type": "Bearer",
"scope": "IMAP.AccessAsUser.All SMTP.Send",
"expires_in": 3599,
"ext_expires_in": 3599,
"access_token": "access_token",
"refresh_token": "refresh_token",
"id_token": "id_token"
}
这是Java代码(使用JavaMail jar 1.6.2)
Properties properties= new Properties();
properties.put("mail.imap.ssl.enable", "true");
properties.put("mail.imap.auth.mechanisms", "XOAUTH2");
//properties.put("mail.imap.sasl.enable", "true"); un-commented still results are same
properties.put("mail.imap.auth.login.disable", "true");
properties.put("mail.imap.auth.plain.disable", "true");
properties.put("mail.debug", "true");
properties.put("mail.debug.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
String userEmail = "[email protected] /cdn-cgi/l/email-protection";
String accessToken = "accessToken";
final Store store = session.getStore("imap");
store.connect("outlook.office365.com","993",userEmail, accessToken);
以下输出:
DEBUG: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [TQBB]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE
NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: XOAUTH2
DEBUG IMAP: protocolConnect login, host=outlook.office365.com, us[email protected] /cdn-cgi/l/email-protection,
password=<non-null>
A1 AUTHENTICATE XOAUTH2 dXNlAQE=
A1 NO AUTHENTICATE failed.
Could not connect to the message store
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:731)
at javax.mail.Service.connect(Service.java:366)
at myproject.EmailReceiver.downloadEmails(EmailReceiver.java:79)
at myproject.EmailReceiver.main(EmailReceiver.java:179)
关注其他帖子无法找到范围https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send https://outlook.office365.com/SMTP.Send在我的蔚蓝中。可能它们是遗留范围。
除了“https://graph.microsoft.com/IMAP.AccessAsUser.All”和“https://graph.microsoft.com/SMTP.send”之外,是否还需要通过 IMAP 连接到 Exchange 联机。或者现有代码的任何问题。
我使用范围 Offline_access%20https%3A%2F%2Foutlook.office365.com%2FIMAP.AccessAsUser.All%20https%3A%2F%2Foutlook.office365.com%2FSMTP.Send 已解决问题
它提供访问邮件的权限,还提供刷新令牌以重新生成访问令牌。
笔记 :
将范围更改为offline_accesshttps://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send https://outlook.office365.com/SMTP.Send解决了问题。