Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IMAP/SMTP authentication with AccountManager token

I'm trying to implement a simple IMAP/SMTP gmail client using the token received from the Android's AccountManager instead of using username and password. I'm trying to use "mail" as authTokenType parameter to getAuthToken().

Google provides this example of SMTP/IMAP with 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

but it is for java. Using it in my Android project there is some import (about SASL) missing. To solve them I use this library http://code.google.com/p/asmack/

The example compile but I notice a problem at runtime

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, [email protected], 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 [email protected] 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)

it seems that asmack uses org.apache.harmony.javax.security.* instead of javax.security.*

Anyone has faced this problem? Google does not seem to consider this possibility in their example, but I think that it should be a common feature for the user.

Thanks.

like image 617
Lorenzo Braghetto Avatar asked Jan 09 '13 14:01

Lorenzo Braghetto


2 Answers

In order to use the ASMACK Authenticator with JavaMail I created a patched version of the library available here: http://www.mannaz.at/codebase/imap-ssl-mail-android/

It is based on http://code.google.com/p/javamail-android/ but supports the ASMACK SASSL authenticator.

This class is missing from the JAR file. Just add it to your source code:

package com.falott.ingresss.util;
public class Log {
    public static boolean loggingEnabled(){
       return false;
    }
}
like image 123
whlk Avatar answered Sep 18 '22 18:09

whlk


From the log, it looks like you have java6 and java7 files mixed up. Can you share your classpath details.

The other possibility is that you dont have the right credentials, either login or pwd is incorrect.

I have tried what you have, and it has worked, should be straight forward.

like image 34
Siddharth Avatar answered Sep 20 '22 18:09

Siddharth