讀取、複製與搜尋(&R)

完成「準備使用 People API」一文中的步驟後,您就可以讀取、複製及搜尋「其他聯絡人」資料。

以下程式碼範例示範如何傳送幾個簡單的要求。如需完整的方法清單,請參閱參考說明文件

列出使用者的「其他聯絡人」

如要取得使用者「其他聯絡人」中的人員清單,請使用下列程式碼:

通訊協定

GET /v1/otherContacts?readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com

Java

ListOtherContactsResponse response = peopleService.otherContacts().list()
    .setReadMask("metadata,names,emailAddresses")
    .execute();

List<Person> otherContacts = response.getOtherContacts();

列出使用者變更的「其他聯絡人」

Java

// Initial request
ListOtherContactsResponse fullSyncResponse = peopleService.otherContacts().list()
    .setReadMask("metadata,names,emailAddresses")
    .setRequestSyncToken(true)
    .execute();
// Fetch all the pages
while (fullSyncResponse.getNextPageToken() != null) {
  fullSyncResponse = peopleService.otherContacts().list()
      .setReadMask("metadata,names,emailAddresses")
      .setRequestSyncToken(true)
      .setPageToken(fullSyncResponse.getNextPageToken())
      .execute();
}

// Some time passes

// Fetch incremental changes using the sync token returned in the last fullSyncResponse.
try {
  ListOtherContactsResponse incrementalSyncResponse = peopleService.otherContacts().list()
      .setReadMask("metadata,names,emailAddresses")
      .setSyncToken(fullSyncResponse.getNextSyncToken())
      .execute();
  for (Person person : incrementalSyncResponse.getOtherContacts()) {
    handlePerson(person);
  }
  
  // Fetch all the pages
  while (!incrementalSyncResponse.getNextPageToken().isEmpty()) {
    incrementalSyncResponse = peopleService.otherContacts().list()
        .setReadMask("metadata,names,emailAddresses")
        .setSyncToken(fullSyncResponse.getNextSyncToken())
        .setPageToken(incrementalSyncResponse.getNextPageToken())
        .execute();
    for (Person person : incrementalSyncResponse.getOtherContacts()) {
      handlePerson(person);
    }
  }
} catch (GoogleJsonResponseException e) {
  if (e.getStatusCode() == 410) {
    // Sync token expired. Make full sync request.
  }
}

void handlePerson(Person person) {
  if (person.getMetadata().getDeleted()) {
    // Handle deleted person
  } else {
    // Handle changed person
  }
}

如要進一步瞭解同步處理行為,請參閱 ListOtherContacts

將「其他聯絡人」複製到「myContacts」群組

如要將「其他聯絡人」複製到「myContacts」群組,請使用以下程式碼:

通訊協定

POST /v1/resource_name:copyOtherContactToMyContactsGroup?copyMask=names,emailAddresses,phoneNumbers HTTP/1.1
Host: people.googleapis.com

Java

Person copiedContact = peopleService
    .otherContacts()
    .copyOtherContactToMyContactsGroup(
        "resource_name",
        new CopyOtherContactToMyContactsGroupRequest()
            .setCopyMask("names,emailAddresses,phoneNumbers"))
    .execute();

搜尋使用者的「其他聯絡人」

如要搜尋使用者的所有「其他聯絡人」,請使用以下程式碼:

通訊協定

// Warmup cache
GET /v1/otherContacts:search?query=&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com

// Send search request after several seconds
GET /v1/otherContacts:search?query=query&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com

Java

// Warmup cache
SearchResponse response = peopleService.otherContacts().search()
    .setQuery("")
    .setReadMask("names,emailAddresses")
    .execute();

// Wait a few seconds
Thread.sleep(5);

// Send search request
SearchResponse response = peopleService.otherContacts().search()
    .setQuery("query")
    .setReadMask("names,emailAddresses")
    .execute();