java– 使用带有accountmanager令牌的imap访问gmail

java– 使用带有accountmanager令牌的imap访问gmail,第1张

概述我正在尝试使用从Android的AccountManager收到的令牌而不是使用用户名和密码来实现IMAPgmail客户端.Google通过oauth2http://code.google.com/p/google-mail-oauth2-tools/source/browse/#svn%2Ftrunk%2Fjava%2Fcom%2Fgoogle%2Fcode%2Fsamples%2Foauth2http://code.google.com/

我正在尝试使用从Android的AccountManager收到的令牌而不是使用用户名和密码来实现IMAP gmail客户端.

Google通过oauth2 http://code.google.com/p/google-mail-oauth2-tools/source/browse/#svn%2Ftrunk%2Fjava%2Fcom%2Fgoogle%2Fcode%2Fsamples%2Foauth2 http://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCode提供了此IMAP示例

public static IMAPStore connectToImap(String host,                                    int port,                                    String userEmail,                                    String oauthToken,                                    boolean deBUG) throws Exception {PropertIEs props = new PropertIEs();props.put("mail.imaps.sasl.enable", "true");props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");props.put(OAuth2SaslClIEntFactory.OAUTH_TOKEN_PROP, oauthToken);Session session = Session.getInstance(props);session.setDeBUG(deBUG);final URLname unusedUrlname = null;IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlname);final String emptyPassword = "";store.connect(host, port, userEmail, emptyPassword);return store;

}

public static voID main(String args[]) throws Exception {if (args.length != 2) {  System.err.println(      "Usage: OAuth2Authenticator <email> <oauthToken>");  return;}String email = args[0];String oauthToken = args[1];initialize();IMAPStore imapStore = connectToImap("imap.gmail.com",                                    993,                                    email,                                    oauthToken,                                    true);System.out.println("Successfully authenticated to IMAP.\n");

}

但是,当我运行上面的代码时,我得到一个“空用户名或密码”的例外.有人可以告诉我如何使用带有xoauth2的imap访问gmail吗?谢谢.

更新2013/02/20,下面是调试日志

 02-19 17:27:20.098  1905: 1905 I/System.out : setDeBUG: JavaMail version 1.4.1     02-19 17:27:20.098  1905: 1905 I/System.out : mail.imap.fetchsize: 16384     02-19 17:27:20.106  1905: 1905 I/System.out : enable SASL     02-19 17:27:20.106  1905: 1905 I/System.out : SASL mechanisms allowed: XOAUTH2     02-19 17:27:21.340  1905: 1905 I/System.out : * OK Gimap ready for requests from 36.224.98.49 z8if14713202igb.53 02-19 17:27:21.348  1905: 1905 I/System.out : A0 CAPABIliTY 02-19 17:27:21.598  1905: 1905 I/System.out : * CAPABIliTY IMAP4rev1 UNSELECT IDLE nameSPACE QUOTA ID XList CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 02-19 17:27:21.598  1905: 1905 I/System.out : A0 OK Thats all she wrote! z8if14713202igb.53 02-19 17:27:21.614  1905: 1905 I/System.out : IMAP DEBUG: AUTH: XOAUTH     02-19 17:27:21.614  1905: 1905 I/System.out : IMAP DEBUG: AUTH: XOAUTH2     02-19 17:27:21.614  1905: 1905 I/System.out : DEBUG: protocolConnect login, host=imap.gmail.com, user=2104176@gmail.com, password=<non-null>     02-19 17:27:21.622  1905: 1905 I/System.out : IMAP SASL DEBUG: Mechanisms: XOAUTH2     02-19 17:27:21.817  1905: 1905 I/System.out : IMAP SASL DEBUG: Failed to create SASL clIEnt: myjavax.security.sasl.SaslException: Cannot instantiate class com.research.oauth.OAuth2SaslClIEntFactory [Caused by java.lang.InstantiationException: can't instantiate class com.research.oauth.OAuth2SaslClIEntFactory]  02-19 17:27:21.817  1905: 1905 I/System.out : A1 LOGIN 2104176@gmail.com "" 02-19 17:27:22.036  1905: 1905 I/System.out : A1 NO Empty username or password. z8if14713202igb.53 02-19 17:27:22.044  1905: 1905 D/test       : javax.mail.AuthenticationFailedException: Empty username or password. z8if14713202igb.53

我使用yor mail.jar和我的应用程序无法创建SASL客户端:myjavax.security.sasl.SaslException:无法实例化类com.research.oauth.OAuth2SaslClIEntFactory,然后app使用空密码登录gmail.
请帮我弄清楚问题,谢谢!

解决方法:

您是否记得在OAuth2ProvIDer中更改您的包名?当我使用该代码进行测试时,我忘了它.

public static final class OAuth2ProvIDer extends ProvIDer {private static final long serialVersionUID = 1L;public OAuth2ProvIDer() {  super("Google OAuth2 ProvIDer", 1.0,        "ProvIDes the XOAUTH2 SASL Mechanism");  put("SaslClIEntFactory.XOAUTH2",      "com.example.testjavamail.OAuth2SaslClIEntFactory");}

}

正如我在另一个回答中所说,我只测试了连接,但它对我有用.

UPDATE

这是我使用的代码,它基本上是示例代码,它真正改变了Java Mail中SASL支持的移植.

public class OAuth2Authenticator {private static final Logger logger = Logger        .getLogger(OAuth2Authenticator.class.getname());private static Session mSession;public static final class OAuth2ProvIDer extends ProvIDer {    private static final long serialVersionUID = 1L;    public OAuth2ProvIDer() {        super("Google OAuth2 ProvIDer", 1.0,                "ProvIDes the XOAUTH2 SASL Mechanism");        put("SaslClIEntFactory.XOAUTH2",                "com.example.testjavamail.OAuth2SaslClIEntFactory");    }}public static voID initialize() {    Security.addProvIDer(new OAuth2ProvIDer());}public static IMAPStore connectToImap(String host, int port,        String userEmail, String oauthToken, boolean deBUG)        throws Exception {    PropertIEs props = new PropertIEs();    props.put("mail.imaps.sasl.enable", "true");    props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");    props.put(OAuth2SaslClIEntFactory.OAUTH_TOKEN_PROP, oauthToken);    Session session = Session.getInstance(props);    session.setDeBUG(deBUG);    final URLname unusedUrlname = null;    IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlname);    final String emptyPassword = "";    store.connect(host, port, userEmail, emptyPassword);    return store;}public static SMTPTransport connectToSmtp(String host, int port,        String userEmail, String oauthToken, boolean deBUG)        throws Exception {    PropertIEs props = new PropertIEs();    props.put("mail.smtp.starttls.enable", "true");    props.put("mail.smtp.starttls.required", "true");    props.put("mail.smtp.sasl.enable", "true");    props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");    props.put(OAuth2SaslClIEntFactory.OAUTH_TOKEN_PROP, oauthToken);    mSession = Session.getInstance(props);    mSession.setDeBUG(deBUG);    final URLname unusedUrlname = null;    SMTPTransport transport = new SMTPTransport(mSession, unusedUrlname);    // If the password is non-null, SMTP trIEs to do AUTH LOGIN.    final String emptyPassword = null;    transport.connect(host, port, userEmail, emptyPassword);    return transport;}public synchronized voID testImap(String user, String oauthToken) {    try {        initialize();        IMAPStore imapStore = connectToImap("imap.gmail.com", 993, user,                oauthToken, true);    } catch (Exception e) {        Log.d("test", e.toString());    }}public class ByteArrayDataSource implements DataSource {    private byte[] data;    private String type;    public ByteArrayDataSource(byte[] data, String type) {        super();        this.data = data;        this.type = type;    }    public ByteArrayDataSource(byte[] data) {        super();        this.data = data;    }    public voID setType(String type) {        this.type = type;    }    public String getContentType() {        if (type == null)            return "application/octet-stream";        else            return type;    }    public inputStream getinputStream() throws IOException {        return new ByteArrayinputStream(data);    }    public String getname() {        return "ByteArrayDataSource";    }    public OutputStream getoutputStream() throws IOException {        throw new IOException("Not Supported");    }}

}

这是来自Java Mail的调试.顺便说一句,发布你的调试日志,它应该有助于解决出错的问题

02-06 10:18:11.805: I/System.out(7434): DEBUG: setDeBUG: JavaMail version 1.4.102-06 10:18:11.905: I/System.out(7434): DEBUG: mail.imap.fetchsize: 1638402-06 10:18:12.025: I/System.out(7434): DEBUG: enable SASL02-06 10:18:12.040: I/System.out(7434): DEBUG: SASL mechanisms allowed: XOAUTH202-06 10:18:12.600: I/System.out(7434): * OK Gimap ready for requests from 2.233.xxx.xxx  2if1471965eej.302-06 10:18:12.605: I/System.out(7434): A0 CAPABIliTY02-06 10:18:12.635: I/System.out(7434): * CAPABIliTY IMAP4rev1 UNSELECT IDLE nameSPACE QUOTA ID XList CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH202-06 10:18:12.635: I/System.out(7434): A0 OK Thats all she wrote! 2if1471965eej.302-06 10:18:12.645: I/System.out(7434): IMAP DEBUG: AUTH: XOAUTH02-06 10:18:12.645: I/System.out(7434): IMAP DEBUG: AUTH: XOAUTH202-06 10:18:12.645: I/System.out(7434): DEBUG: protocolConnect login, host=imap.gmail.com, user=xxx@gmail.com, password=<non-null>02-06 10:18:12.650: I/System.out(7434): IMAP SASL DEBUG: Mechanisms: XOAUTH202-06 10:18:12.695: I/System.out(7434): IMAP SASL DEBUG: SASL clIEnt XOAUTH202-06 10:18:12.695: I/System.out(7434): A1 AUTHENTICATE XOAUTH202-06 10:18:12.720: I/System.out(7434): + 02-06 10:18:12.720: I/System.out(7434): IMAP SASL DEBUG: challenge:  :02-06 10:18:12.730: I/System.out(7434): IMAP SASL DEBUG: callback length: 102-06 10:18:12.730: I/System.out(7434): IMAP SASL DEBUG: callback 0: myjavax.security.auth.callback.nameCallback@41760f7802-06 10:18:12.730: I/System.out(7434): IMAP SASL DEBUG: response: user=xxx@gmail.comauth=Bearer ya29.... :02-06 10:18:12.735: I/System.out(7434): dXNlcj1hbGVhbGVtYXp6b3R0aUBnbWFpbC5jb20BYXV0aD1CZWFyZXIgeWEyOS5BSEVTNlpRYklPeU8xU09sR01WSEO3X2tqVzlVdzNYY1RvODBtQ0hyWFVacjRsYlhIDwEB02-06 10:18:12.870: I/System.out(7434): * CAPABIliTY IMAP4rev1 UNSELECT IDLE nameSPACE QUOTA ID XList CHILDREN X-GM-EXT-1 UIdplUS COMPRESS=DEFLATE02-06 10:18:12.870: I/System.out(7434): A1 OK xxx@gmail.com My name authenticated (Success)02-06 10:18:12.870: I/System.out(7434): A2 CAPABIliTY02-06 10:18:13.160: I/System.out(7434): * CAPABIliTY IMAP4rev1 UNSELECT IDLE nameSPACE QUOTA ID XList CHILDREN X-GM-EXT-1 UIdplUS COMPRESS=DEFLATE02-06 10:18:13.160: I/System.out(7434): A2 OK Success
总结

以上是内存溢出为你收集整理的java – 使用带有accountmanager令牌的imap访问gmail全部内容,希望文章能够帮你解决java – 使用带有accountmanager令牌的imap访问gmail所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1106409.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存