【问题标题】:Connect to microsoft Outlook.com through IMAP using XOauth2 in java在 java 中使用 XOauth2 通过 IMAP 连接到 Microsoft Outlook.com
【发布时间】:2013-10-08 01:21:13
【问题描述】:

Microsoft 发布了a new blog entry,介绍了其使用 OAuth2 获取电子邮件的新 IMAP 功能。但是,我几乎没有使用 IMAP 的经验,并且在使用 IMAP 中的 OAuth 访问令牌时遇到问题。 我曾经使用javamail for android,并且使用电子邮件和密码,我能够毫无问题地访问电子邮件。但是,我不知道如何使用访问令牌通过 javamail 进行连接,我什至不确定这是否可能。 关于如何使用 IMAP 和 OAuth2 访问“imap-mail.outlook.com”的任何想法?

【问题讨论】:

    标签: java android oauth-2.0 jakarta-mail imap


    【解决方案1】:

    解决方案是放弃 javamail 并将其替换为 apache commons net 库。下面介绍的方法使用 Outlook imap 服务器进行身份验证。 Apache commons lib 不像 javamail 那样用户友好,但它似乎是要走的路

    private boolean sendBase64Credentials(IMAPSClient client) {
        String base64Encoded;
    
        try {
            // Outlook.com specification
            // user={user@domain.com}^Aauth=Bearer {access token}^A^A
            byte[] binLogin = ("user=" + user + "\u0001auth=Bearer "
                    + oauthToken + "\u0001\u0001").getBytes("utf-8");
            base64Encoded = Base64.encodeBase64StringUnChunked(binLogin);
        } catch (UnsupportedEncodingException e) {
            return false;
        }
    
        try {
            int code = client.sendData(base64Encoded);
            switch (code) {
            case IMAPReply.OK:
                return true;
            case IMAPReply.BAD:
            case IMAPReply.CONT:
            case IMAPReply.NO:
            default:
                System.out.println("return = ?");
            }
            System.out.println(client.getReplyString());
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    private boolean authenticateXoauth2(IMAPSClient client) {
        try {
            int code = client.sendCommand("AUTHENTICATE XOAUTH2");
            switch (code) {
            case IMAPReply.CONT:
                return sendBase64Credentials(client);
            case IMAPReply.OK:
            case IMAPReply.BAD:
            case IMAPReply.NO:
            default:
                System.out.println("ERRO");
            }
    
        } catch (Exception e) {
            System.out.println("Erro: " + e.getMessage());
        }
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2016-06-30
      • 2014-07-04
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多