6

我正在尝试使用从 Android 的 AccountManager 收到的令牌而不是使用用户名和密码来实现一个简单的 IMAP/SMTP gmail 客户端。我正在尝试使用“邮件”作为 getAuthToken() 的 authTokenType 参数。

Google 提供了这个带有 oauth2 的 SMTP/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

但它适用于java。在我的 Android 项目中使用它时缺少一些导入(关于 SASL)。为了解决它们,我使用这个库http://code.google.com/p/asmack/

该示例编译但我注意到运行时出现问题

10-04 10:05:44.715: I/System.out(1226): DEBUG: setDebug: JavaMail version 1.4.1
10-04 10:05:44.750: I/System.out(1226): DEBUG: mail.imap.fetchsize: 16384
10-04 10:05:44.750: I/System.out(1226): DEBUG: enable SASL
10-04 10:05:44.750: I/System.out(1226): DEBUG: SASL mechanisms allowed: XOAUTH2
10-04 10:05:46.137: I/System.out(1226): * OK Gimap ready for requests from 84.221.66.29 o42if871216eef.60
10-04 10:05:46.137: I/System.out(1226): A0 CAPABILITY
10-04 10:05:46.805: I/System.out(1226): * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2
10-04 10:05:46.805: I/System.out(1226): A0 OK Thats all she wrote! o42if871216eef.60
10-04 10:05:46.805: I/System.out(1226): IMAP DEBUG: AUTH: XOAUTH
10-04 10:05:46.805: I/System.out(1226): IMAP DEBUG: AUTH: XOAUTH2
10-04 10:05:46.813: I/System.out(1226): DEBUG: protocolConnect login, host=imap.gmail.com, user=xxx.xxx@gmail.com, password=<non-null>
10-04 10:05:46.813: I/dalvikvm(1226): Could not find method javax.security.sasl.Sasl.createSaslClient, referenced from method com.sun.mail.imap.protocol.IMAPSaslAuthenticator.authenticate
10-04 10:05:46.813: W/dalvikvm(1226): VFY: unable to resolve static method 44358: Ljavax/security/sasl/Sasl;.createSaslClient ([Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljavax/security/auth/callback/CallbackHandler;)Ljavax/security/sasl/SaslClient;
10-04 10:05:46.813: D/dalvikvm(1226): VFY: replacing opcode 0x77 at 0x0050
10-04 10:05:46.813: W/dalvikvm(1226): VFY: unable to resolve exception class 5975 (Ljavax/security/sasl/SaslException;)
10-04 10:05:46.813: W/dalvikvm(1226): VFY: unable to find exception handler at addr 0x83
10-04 10:05:46.844: W/dalvikvm(1226): VFY:  rejected Lcom/sun/mail/imap/protocol/IMAPSaslAuthenticator;.authenticate ([Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z
10-04 10:05:46.844: W/dalvikvm(1226): VFY:  rejecting opcode 0x0d at 0x0083
10-04 10:05:46.844: W/dalvikvm(1226): VFY:  rejected Lcom/sun/mail/imap/protocol/IMAPSaslAuthenticator;.authenticate ([Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z
10-04 10:05:46.844: W/dalvikvm(1226): Verifier rejected class Lcom/sun/mail/imap/protocol/IMAPSaslAuthenticator;
10-04 10:05:46.844: I/System.out(1226): IMAP DEBUG: Can't load SASL authenticator: java.lang.ClassNotFoundException: com.sun.mail.imap.protocol.IMAPSaslAuthenticator
10-04 10:05:46.844: I/System.out(1226): A1 LOGIN xxx.xxx@gmail.com anonymous
10-04 10:05:48.137: I/System.out(1226): A1 NO [AUTHENTICATIONFAILED] Invalid credentials (Failure)
10-04 10:05:48.164: W/System.err(1226): javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure)
10-04 10:05:48.176: W/System.err(1226):         at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:566)
10-04 10:05:48.176: W/System.err(1226):         at javax.mail.Service.connect(Service.java:288)
10-04 10:05:48.176: W/System.err(1226):         at xxx.xxx.xxx.OAuth2Authenticator.connectToImap(OAuth2Authenticator.java:111)
10-04 10:05:48.180: W/System.err(1226):         at xxx.xxx.xxx.OAuth2Authenticator.initialize(OAuth2Authenticator.java:60)
10-04 10:05:48.180: W/System.err(1226):         at xxx.xxx.xxx.TR_Incoming.onTimeout(TR_Incoming.java:106)
10-04 10:05:48.180: W/System.err(1226):         at xxx.xxx.xxx.AlarmThread$1.handleMessage(AlarmThread.java:80)
10-04 10:05:48.180: W/System.err(1226):         at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 10:05:48.180: W/System.err(1226):         at android.os.Looper.loop(Looper.java:137)
10-04 10:05:48.180: W/System.err(1226):         at xxx.xxx.xxx.AlarmThread.run(AlarmThread.java:94)

似乎 asmack 使用 org.apache.harmony.javax.security.* 而不是 javax.security.*

有人遇到过这个问题吗?Google 在他们的示例中似乎没有考虑这种可能性,但我认为这应该是用户的共同特征。

谢谢。

4

2 回答 2

4

为了将 ASMACK Authenticator 与 JavaMail 一起使用,我创建了一个可用的库的补丁版本:http: //www.mannaz.at/codebase/imap-ssl-mail-android/

它基于http://code.google.com/p/javamail-android/但支持 ASMACK SASSL 身份验证器。

JAR 文件中缺少此类。只需将其添加到您的源代码中:

package com.falott.ingresss.util;
public class Log {
    public static boolean loggingEnabled(){
       return false;
    }
}
于 2013-02-09T20:00:45.667 回答
1

从日志来看,您似乎混淆了 java6 和 java7 文件。你能分享你的类路径细节吗?

另一种可能性是您没有正确的凭据,登录或密码不正确。

我已经尝试过你所拥有的,它已经奏效了,应该是直截了当的。

于 2013-02-03T17:36:56.043 回答