我目前通過OAuth 2.0和所謂的服務帳戶執行訪問谷歌聯繫人。該服務帳戶是爲「[email protected]」等普通用戶生成的。通過OAuth 2.0又名服務的私有密鑰訪問谷歌聯繫人API帳戶
的代碼來生成OAuth 2.0憑據是:
public static GoogleCredential getCredentials() throws GeneralSecurityException, IOException {
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SingleUserCredentials.SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes("https://www.google.com/m8/feeds")
.setServiceAccountPrivateKeyFromP12File(new File(SingleUserCredentials.SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
credential.refreshToken();
return credential;
}
我再試圖檢索通過聯繫人:
ContactsService myService = new ContactsService(
"myApp");
myService.setOAuth2Credentials(getCredentials());
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
Query myQuery = new Query(feedUrl);
ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);
// Print the results
for (ContactEntry entry : resultFeed.getEntries()) {
System.out.println(entry.getName().getFullName().getValue());
System.out.println("Updated on: " + entry.getUpdated().toStringRfc822());
}
的問題是,我沒有得到任何一個單一的接觸從我的帳戶。 Feed始終爲空。沒有錯誤。沒有。
在訪問谷歌企業應用套件通過它很好地工作同樣的方法管理域。
我想知道在使用p12密鑰文件和服務帳戶時,聯繫人Api是否支持普通(又名@ gmail.com)帳戶的OAuth 2.0。
嗨匿名,謝謝你的回答。但是,您正在描述基於密鑰的OAuth 2.0訪問域的方法。如上所述,這對我來說很好。我真正想要的是通過像[email protected]這樣的普通用戶訪問聯繫人的解決方案 - 沒有谷歌管理域。我希望我的問題在這方面是有道理的。 –