我正在尝试使用从 Android 的 AccountManager 收到的令牌而不是使用用户名和密码来实现 IMAP gmail 客户端。
Google 提供了这个带有 oauth2 的 IMAP 示例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
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");
}
但是,当我运行上面的代码时,我得到了“空用户名或密码”的异常。有人可以告诉我如何使用 imap 和 xoauth2 访问 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
我使用你的 mail.jar 和我的应用程序无法创建 SASL 客户端:myjavax.security.sasl.SaslException: 无法实例化类 com.research.oauth.OAuth2SaslClientFactory,然后应用程序使用空密码登录 gmail。请帮我解决问题,谢谢!