imap.gmail.comからメッセージを取得しようとしています。
まず、AccountManager から認証トークンを取得します。
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 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] 資格情報が無効です (失敗)」。
助言がありますか?
編集:ああ、すべて正常に動作しています。おそらく、認証サーバーの問題です..