如何使用OAUTH2访问outlook.office365.com IMAP表单Java



由于微软宣布,使用基本身份验证访问Outlook IMAP邮箱很快将不再可行,我正试图找出如何使用Java中的OAUTH2正确打开IMAP邮箱。但我总是得到错误代码";A1 NO AUTHENTICATE失败">

我正在做的是:

我有一种生成OAUTH2访问令牌的方法:

public String getAuthToken(String tanantId,String clientId,String client_secret) throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/" + tanantId + "/oauth2/v2.0/token");
String scopes = "https://outlook.office365.com/.default";
String encodedBody = "client_id=" + clientId + "&scope=" + scopes + "&client_secret=" + client_secret
+ "&grant_type=client_credentials";
loginPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_FORM_URLENCODED));
loginPost.addHeader(new BasicHeader("cache-control", "no-cache"));
CloseableHttpResponse loginResponse = client.execute(loginPost);
InputStream inputStream = loginResponse.getEntity().getContent();
byte[] response = readAllBytes(inputStream);
ObjectMapper objectMapper = new ObjectMapper();
JavaType type = objectMapper.constructType(
objectMapper.getTypeFactory().constructParametricType(Map.class, String.class, String.class));
Map<String, String> parsed = new ObjectMapper().readValue(response, type);
return parsed.get("access_token");
}

生成的令牌似乎是有效的,因为jwt.ms允许我解码令牌。

我尝试使用以下访问令牌通过XOAUTH2访问邮箱:

Properties props = new Properties();
props.put("mail.store.protocol", "imap");
props.put("mail.imap.host", "outlook.office365.com");
props.put("mail.imap.port", "993");
props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.starttls.enable", "true");
props.put("mail.imap.auth", "true");
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
props.put("mail.imap.user", mailAddress);
props.put("mail.debug", "true");
props.put("mail.debug.auth", "true");

// open mailbox....
String token = getAuthToken(tanantId,clientId,client_secret);
Session session = Session.getInstance(props);
session.setDebug(true);
Store store = session.getStore("imap");
store.connect("outlook.office365.com", mailAddress, token);

但结果总是AuthenticationFailedException:

* OK The Microsoft Exchange IMAP4 service is ready. [...............AA==]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE 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, user=xxx@yyy.com, password=<non-null>
A1 AUTHENTICATE XOAUTH2 ....E=
A1 NO AUTHENTICATE failed.
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.

从这个类似的问题中,我现在怀疑我的访问令牌实际上访问邮箱的权限太少。

我该如何澄清这一点?例如,当我解码令牌时,我可以看到它不包括scproles属性。这是否表明令牌是错误的?

代码示例是正确的。问题是所提供的servicePrincipal缺少权限。要在office365中创建servicePrincipal,需要使用objectId。这些任务只能由管理员使用PowerShell脚本执行。这些都与Java无关。Java API是直接的。

对于每个人来说;A1没有认证失败";,即使您已经尝试了此线程和链接线程中的所有内容:我们现有的javamail代码,在微软删除它之前,它与基本的auth完美地工作,总是得到"A1"没有认证失败";错误,尽管我们尝试了这里的其他一切和链接的线程。最终使其发挥作用的是以下变化:

imapStore = sess.getStore("imaps");

imapStore = sess.getStore("imap");

因此,如果你习惯于连接到";imaps";用基本的auth;imap";在拔出其余头发之前先试试…;-(

相关内容

最新更新