Connecting to GMAIL through IMAP using OAuth and the Google account token provided by Android's AccountManager

StackOverflow https://stackoverflow.com/questions/5305605

Question

I want to access GMAIL through IMAP from android, using OAuth. As shown here: http://code.google.com/apis/gmail/oauth/protocol.html#imap

I tried to use the auth token returned by the AccountManager as the AUTHENTICATE parameter, and I was not able to authenticate.

I am using this code to get the token:

AccountManager accountManager = AccountManager.get(this);
Account[] accounts = accountManager.getAccountsByType("com.google");

AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(accounts[0], "android", null, (Activity)this, null, null); 
Bundle authTokenBundle = accountManagerFuture.getResult();
String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

And after the IMAP connection, I execute this command:

"AUTHENTICATE XOAUTH " + authToken

And I receive a invalid argument response.

What am I doing wrong? Is there really a relation between the OAuth token and the Account token? If there is not, how I could get the XOAUTH token from the account token.

Thanks.

Was it helpful?

Solution

Are you sure you should be using "android" rather than "mail" when supplying the authTokenType in getAuthToken()? Alternately you can use "ah" which isn't listed but appears to be asking for access to everything.

http://code.google.com/apis/gdata/faq.html#clientlogin

You also might want to try http://code.google.com/p/google-api-java-client/ as it supports the android AccountManager authentication type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top