【问题标题】:Cannot connect to Gmail IMAP using XOAUTH on Android无法在 Android 上使用 XOAUTH 连接到 Gmail IMAP
【发布时间】:2012-07-17 05:24:14
【问题描述】:

我正在构建一个使用 Gmail 来备份一些数据的应用。我使用 XOAUTH 连接到 Gmail 并获得了令牌和密码。但我无法连接到 Gmail 的 IMAP 服务。我按照http://code.google.com/p/google-mail-xoauth-tools/wiki/JavaSampleCode的例子:

Properties props = new Properties();
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH");
props.put(OAUTH_TOKEN_PROP, oauthToken);
props.put(OAUTH_TOKEN_SECRET_PROP, oauthTokenSecret);
props.put(CONSUMER_KEY_PROP, Const.CONSUMER_KEY);
props.put(CONSUMER_SECRET_PROP, Const.CONSUMER_SECRET);
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;

运行时报如下异常。

javax.mail.MessagingException: * BYE [UNAVAILABLE] Temporary System Error;
   nested exception is:
    com.sun.mail.iap.ConnectionException: * BYE [UNAVAILABLE] Temporary System Error
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:569)
    at javax.mail.Service.connect(Service.java:288)
    at com.doodlemobile.zy.finenote.OAuthHelperActivity$XoauthAuthenticator.connectToImap(OAuthHelperActivity.java:565)
    at com.doodlemobile.zy.finenote.OAuthHelperActivity$ConnectGmailTask.doInBackground(OAuthHelperActivity.java:484)
    at com.doodlemobile.zy.finenote.OAuthHelperActivity$ConnectGmailTask.doInBackground(OAuthHelperActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: com.sun.mail.iap.ConnectionException: * BYE [UNAVAILABLE] Temporary System Error
    at com.sun.mail.iap.Protocol.handleResult(Protocol.java:346)
    at com.sun.mail.imap.protocol.IMAPProtocol.login(IMAPProtocol.java:336)
    at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:615)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:549)
    ... 11 more

我用的是SUN的javamail。有人会帮助我吗?

【问题讨论】:

  • 您是否使用 AccountManager 获取令牌?如果是这样,您是如何获得秘密令牌的?
  • 您是否启用了会话调试?抛出异常的地方有什么输出吗?
  • 您是如何设法在 Android 上获得“oauthTokenSecret”的?
  • 抱歉耽搁了,我用signpost来获取oauthToken和oauthTokenSecret。

标签: java android oauth imap gmail-imap


【解决方案1】:

我也面临同样的问题。

启用调试 (session.setDebug(true)) 后,我注意到在显示临时系统错误之前我收到了 ClassNotFoundException

08-06 14:38:59.148: I/System.out(3139): DEBUG IMAP: Can't load SASL authenticator: java.lang.ClassNotFoundException: com.sun.mail.imap.protocol.IMAPSaslAuthenticator
08-06 14:38:59.148: I/System.out(3139): DEBUG IMAP: LOGIN command trace suppressed
08-06 14:39:00.265: I/System.out(3139): DEBUG IMAP: LOGIN command result: * BYE [UNAVAILABLE] Temporary System Error
08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139): * BYE [UNAVAILABLE] Temporary System Error
08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139): javax.mail.MessagingException: * BYE [UNAVAILABLE] Temporary System Error;
08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139):   nested exception is:
08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139):  com.sun.mail.iap.ConnectionException: * BYE [UNAVAILABLE] Temporary System Error
08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139):  at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:668)

查看 JavaMail 源代码似乎是因为它正在尝试加载依赖于非 android java 资源的 com.sun.mail.imap.protocol.IMAPSaslAuthenticator。

javax.security.sasl.*

【讨论】:

  • 那么我们能做些什么来解决这个问题?
  • 我目前正在研究替代解决方案,并将报告任何发现。目前正在考虑在 K9 Mail 中实施 XOAUTH 作为概念证明。
  • 您好,参加聚会有点晚了,但我让 sasl 使用了适用于 android 的 java 邮件。这很简单,android(和 android 的 java 邮件)只是没有 sasl、javax.security.sasl.* 和 javax.security.auth.callback.* 的类。我将类添加到 java 邮件,更改命名空间,修改了一些引用是代码,它正在使用 imap(没有尝试使用 smtp,因为我已经让它跳过 sasl)。不确定这是否是最好的方法,但好吧...如果您仍然感兴趣,请告诉我,我明天会发布代码。
  • 嗨,亚历克斯:你提到的方法有效!我刚刚添加了缺少的类并将其重命名为包,它工作正常。谢谢!!
  • 嗨@Chris.Zou 你能参考一下如何添加类和重命名它们的包吗?文件扩展名为 .class 并已编译,因此无法重命名包。提前致谢。
【解决方案2】:

其他人也experienced this error

试试他们给出的建议:

我取消了注册,然后在每个会话之间注册了安全提供程序,并决定修复它。不知道为什么...

我确实让它工作了。事实证明,我需要在我正在使用的系统上安装更新版本的 Java。升级后,一切都开始正常工作了。

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 2020-04-25
    • 1970-01-01
    • 2016-09-01
    • 1970-01-01
    • 2011-07-10
    • 2011-12-26
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多