gpt4 book ai didi

android - 使用 AccountManager token 的 IMAP/SMTP 身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:01:04 25 4
gpt4 key购买 nike

我正在尝试使用从 Android 的 AccountManager 接收到的 token 而不是使用用户名和密码来实现一个简单的 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.*

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

谢谢。

最佳答案

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

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

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

package com.falott.ingresss.util;
public class Log {
public static boolean loggingEnabled(){
return false;
}
}

关于android - 使用 AccountManager token 的 IMAP/SMTP 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14237572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com