我目前正在尝试使用服务帐户及其P12密钥 连接到简单Gmail用户的Imap服务器。
我成功实现了域拓扑委托服务 如上所述的帐户 http://code.google.com/p/java-gmail-imap/wiki/GmailAndXOAUTH2。
但是我无法连接到普通的旧Gmail帐户。
生成OAuth 2.0令牌的代码是:
public static String getAccessToken(String email) throws Exception {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SingleUserCredentials.SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes("https://mail.google.com/")
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SingleUserCredentials.SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.setServiceAccountUser(SingleUserCredentials.EMAIL)
.build();
credential.refreshToken();
return credential.getAccessToken();
}
然后我连接到ImapStore,如:
String authToken = getAccessToken(SingleUserCredentials.EMAIL);
OAuth2Authenticator.initialize();
IMAPStore imapSslStore = OAuth2Authenticator.connectToImap(
"imap.gmail.com",
993,
SingleUserCredentials.EMAIL,
authToken,
true);
我总是在线程“main”中获得异常 javax.mail.AuthenticationFailedException:[ALERT]凭据无效 (失败)。
至少有两件事情很奇怪: