Android 使用oauth、imap和Authtoken获取gmail收件箱
嗨,我正在尝试通过IMAP获取gmail收件箱,使用的是从Android的AccountManager收到的令牌,而不是用户名和密码 我正在试用Google的SMTP/IMAP示例和oauth2 我可以从下面的代码中获取令牌Android 使用oauth、imap和Authtoken获取gmail收件箱,android,oauth,oauth-2.0,imap,gmail-imap,Android,Oauth,Oauth 2.0,Imap,Gmail Imap,嗨,我正在尝试通过IMAP获取gmail收件箱,使用的是从Android的AccountManager收到的令牌,而不是用户名和密码 我正在试用Google的SMTP/IMAP示例和oauth2 我可以从下面的代码中获取令牌 AccountManager accountMan = AccountManager.get(this); accountMan.invalidateAuthToken("com.google", token);//old key AccountMa
AccountManager accountMan = AccountManager.get(this);
accountMan.invalidateAuthToken("com.google", token);//old key
AccountManager accountManager = AccountManager.get(this);
Account[] acc = accountManager.getAccountsByType("com.google");
futur = accountManager.getAuthToken(acc[0], "oauth2:https://www.googleapis.com/auth/analytics.readonly" , null, this, new OnTokenAcquired(), null);
private class OnTokenAcquired implements AccountManagerCallback<Bundle>{
@Override
public void run(AccountManagerFuture<Bundle> result)
{
try
{
Bundle bundle = result.getResult();
token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
}
catch (Exception e)
{
Log.e("test","token"+ e.getMessage());
}
}
}
在上面的代码中,我无法连接到存储。我不知道我会错在哪里。请任何人都有相关的工作样本代码。请帮忙
谢谢 什么是错误/异常?没有错误或任何异常,即使是更新问题的登录尝试块也没有。您应该检查此应用程序的源。。也一样。。它使用K9 IMAP库。您应该使用库来避免重新发明轮子
try{
IMAPStore imapStore = connectToImap("imap.gmail.com",
993,
user,
oauthToken,
true);
Log.e("Successfully authenticated to IMAP"," ");
}
catch (Exception ex)
{
ex.printStackTrace();
}
public static IMAPStore connectToImap(String host,
int port,
String userEmail,
String oauthToken,
boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
Session session = Session.getInstance(props);
final URLName unusedUrlName = null;
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
final String emptyPassword = "";
store.connect(host, port, userEmail, emptyPassword);
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message[] messages = inbox.getMessages();
for (Message message : messages)
{
try
{
Log.e("inbox"," "+message.getSubject());
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
}
return store;
}
}