Outlook.office365.com IMAP OAuth2身份验证问题

时间:2020-04-02 09:48:21

标签: azure office365 azure-ad-graph-api

由于MS为dropping basic-auth support on IMAP this fall(2020年),所以我尝试设置OAuth2。

我已经成功设置了一个应用程序,并且能够使用msal4j lib检索访问令牌。我使用ROPC flow

我的令牌似乎还可以,并且具有所需的作用域https://graph.microsoft.com/IMAP.AccessAsUser.All

尽管如此,在IMAP服务器上的登录尝试仍然失败,没有其他错误消息(AUTHENTICATE failed)。

public class IMAPMailReceiverTest {

    public static final void main(String[] strg) throws Exception {
        PublicClientApplication app = PublicClientApplication.builder("[app-id]")
                .authority("https://login.microsoftonline.com/[tenant-id]/").build();

        Set<String> scope = new HashSet<>();
        scope.add("https://graph.microsoft.com/IMAP.AccessAsUser.All");

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(UserNamePasswordParameters
                .builder(scope, "test@domain.com", "[password]".toCharArray()).build());

        future.handle((res, ex) -> {
            if (ex != null) {
                System.out.println("message - " + ex.getMessage());
                return "Unknown!";
            }
            System.out.println("Access Token - " + res.accessToken());
            System.out.println("ID Token - " + res.idToken());

            try {
                Properties props = new Properties();
                props.put("mail.imap.ssl.enable", "true");
                props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
                props.put("mail.imaps.auth.plain.disable", true);
                Session session = Session.getInstance(props);
                Store store = session.getStore("imaps");
                store.connect("Outlook.office365.com", "test@domain.com", res.accessToken());
            } catch (Exception e1) {
                e1.printStackTrace();
            }

            return res;
        });

        future.join();
        TimeUnit.SECONDS.sleep(300);
    }

}

有没有人使用IMAP / OAuth2工作流为Office365帐户成功获取电子邮件?

(我正在使用msal4j lib和Java Mail的最新版本。)

0 个答案:

没有答案