IMAP/SMTP authentication with AccountManager token
Asked Answered
A

2

6

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.

Apheliotropic answered 9/1, 2013 at 14:35 Comment(1)
The problem arises from the fact that com.sun.mail.imap.protocol.IMAPSaslAuthenticator references javax.security.sasl classes which are missing. You should possibly get the javamail's sources, for example, from here, and change imports in IMAPSaslAuthenticator to org.apache.harmony.javax.security.sasl classes, available in asmack, as has been noted already.Ultimatum
U
4

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;
    }
}
Unpaidfor answered 9/2, 2013 at 20:0 Comment(4)
Hi, I followed the instructions on your side, but I get this error when calling connectToImap: 08-24 22:22:51.967: W/dalvikvm(31957): VFY: unable to resolve static method 4410: Lcom/falott/ingresss/util/Log;.loggingEnabled ()Z 08-24 22:22:51.967: W/dalvikvm(31957): VFY: unable to resolve static method 4410: Lcom/falott/ingresss/util/Log;.loggingEnabled ()Z Any ideas?Newhall
Yes, I forgot to remove that reference. Just create a static dummy method in a class under that package and you should be fine.Unpaidfor
Hi, I would like to rebuild the patched version with very latest java mail (to fix some GMail / IMAP bugs) could you please let me (us) know how to do?Majesty
Mannaz, could you please contact me? I would like to achieve the same thing as you did with the ASMACK authenticator, but with very latest Java Mail. Thanks beforehandMajesty
C
1

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.

Contraoctave answered 3/2, 2013 at 17:36 Comment(2)
We are talking of the android system here.Unpaidfor
Is it possible he has picked up the wrong mail jar files and included it in the project ? The class that is "could not find, is only in java7". Also the example he is looking at, most probably is not android working example. Its a java working example. I would suspect..Contraoctave

© 2022 - 2024 — McMap. All rights reserved.