使用OAuth和Android帐户管理器提供的Google帐户令牌通过IMAP连接到GMAIL

时间:2011-03-14 22:55:18

标签: android gmail imap token accountmanager

我想使用OAuth通过IMAP从Android访问GMAIL。如下图所示: http://code.google.com/apis/gmail/oauth/protocol.html#imap

我尝试使用AccountManager返回的身份验证令牌作为AUTHENTICATE参数,但我无法进行身份验证。

我正在使用此代码获取令牌:

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();

在IMAP连接之后,我执行以下命令:

"AUTHENTICATE XOAUTH " + authToken

我收到无效的辩论回复。

我做错了什么? OAuth令牌和帐户令牌之间是否存在关系?如果没有,我如何从帐户令牌中获取XOAUTH令牌。

感谢。

1 个答案:

答案 0 :(得分:2)

在[{1}}中提供authTokenType时,您确定应该使用“android”而不是“mail”吗?或者你可以使用“啊”,它没有列出,但似乎要求访问所有内容。

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

您也可以尝试http://code.google.com/p/google-api-java-client/,因为它支持Android AccountManager身份验证类型。

相关问题