![](/img/trans.png)
[英]How to do a partial update on contacts using Google API Java client?
[英]How to authenticate a java application for the Google Contacts API?
我有一個運行了幾年的Java應用程序,它已將某些聯系人從某些數據庫同步到客戶的gmail帳戶中。
幾天前,此操作停止了,可能是因為Google 停止了支持簡單的用戶名/密碼身份驗證。
第一個問題:假設現在唯一支持的身份驗證是OAuth2,是否正確?
假設是這種情況,我不知道應該使用哪種授權方案/流程。 我無法顯示對話框-這是一種服務類型的應用程序,它只能訪問我控制的一個特定客戶帳戶。 我認為我應該使用“ 服務帳戶” ,但是文檔中指出:
通常,當應用程序使用Google API來處理其自身的數據而不是用戶的數據時,該應用程序將使用服務帳戶。
但我想使用用戶數據-具體來說就是“聯系人”。
對於這種情況,文檔說明如下:
如果您擁有Google Apps域(例如,如果您使用Google Apps for Work),則Google Apps域的管理員可以授權應用程序代表Google Apps域中的用戶訪問用戶數據。 例如,使用Google Calendar API將事件添加到Google Apps域中所有用戶的日歷的應用程序將使用服務帳戶代表用戶訪問Google Calendar API。 授權服務帳戶代表域中的用戶訪問數據有時稱為“將域范圍的權限委派給服務帳戶”。
但是,我沒有Google Apps域。
那么現在怎么辦?
第一個問題:假設現在唯一支持的身份驗證是OAuth2,是否正確?
是
那么現在怎么辦?
與用戶名密碼最接近的類比是刷新令牌(它只是一個字符串)。 IE:-
此處介紹了獲取刷新令牌所需的步驟。 如何在沒有用戶干預的情況下授權應用程序(網絡或已安裝)? (規范?)
該答案還包含指向Google文檔的鏈接,該鏈接說明了如何使用刷新令牌來獲取訪問令牌。
這是一些代碼(基於pinoyyid的建議):
String CLIENT_ID = "see instructions in accepted answer";
String CLIENT_SECRET = "see instructions in accepted answer";
String REFRESH_TOKEN = "see instructions in accepted answer";
Builder builder = new GoogleCredential.Builder();
builder.setTransport(GoogleNetHttpTransport.newTrustedTransport());
builder.setJsonFactory(JacksonFactory.getDefaultInstance());
builder.setClientSecrets(CLIENT_ID, CLIENT_SECRET);
Credential credential = builder.build();
credential.setRefreshToken(REFRESH_TOKEN);
credential.refreshToken(); // gets the access token, using the refresh token
ContactsService contactsService.setOAuth2Credentials(credential);
Query query = new Query(new URL("https://www.google.com/m8/feeds/contacts/default/full"));
query.setMaxResults(10_000);
ContactFeed allContactsFeed = contactsService.getFeed(query, ContactFeed.class);
LOGGER.log(Level.INFO, allContactsFeed.getTitle().getPlainText());
for(ContactEntry contact : allContactsFeed.getEntries())
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.