我正在尝试从imap.gmail.com获取消息。
首先,我从 AccountManager 获得一个 authtoken:
public static String[] getAuthToken(String scope){
AccountManager accountManager = AccountManager.get(MyApp.getAppContext());
accountManager.invalidateAuthToken("com.google", null);
Account[] accounts = accountManager.getAccounts();
Account myAccount = null;
Bundle options = null;
String[] resultStrings = new String[2];
String username = null;
String userToken = null;
for (Account account : accounts) {
if (account.type.equalsIgnoreCase("com.google")){
Log.v(android.content.Context.NOTIFICATION_SERVICE, "Get account..");
myAccount = account;
username = myAccount.name;
options = new Bundle();
AccountManagerFuture<Bundle> future = accountManager.getAuthToken(
myAccount, // Account retrieved using getAccountsByType()
scope, //"oauth2:https://mail.google.com",
options, // Authenticator-specific options
StartActivity.getActivity(), // Your activity
new OnTokenAcquired(), // Callback called when a token is successfully acquired
new Handler(Looper.getMainLooper(), new OnError()));
try {
Bundle bundle = future.getResult();
userToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);
} catch (OperationCanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
resultStrings[0] = username;
resultStrings[1] = userToken;
return resultStrings;
}
范围在这里 - “oauth2: https://mail.google.com ” 之后,我使用这个库:http://www.mannaz.at/codebase/imap-ssl-mail-android/打开 IMAPStore:
IMAPStore store = null;
try {
store = OAuth2Authenticator.connectToImap(
"imap.gmail.com",
993,
username,
userToken,
true);
Log.v("notification", "Connected");
} catch (Exception e1) {
e1.printStackTrace();
}
但我得到了错误:
“javax.mail.AuthenticationFailedException:[ALERT] 凭据无效(失败)”。
有什么建议么?
编辑:哦,一切正常,也许,这只是身份验证服务器上的麻烦..