Đọc, sao chép và tìm kiếm "Danh bạ khác"

Sau khi hoàn tất các bước trong Sẵn sàng sử dụng API mọi người, bạn có thể đọc, sao chép và tìm kiếm dữ liệu về "Danh bạ khác".

Các mã mẫu sau đây minh hoạ cách gửi một số yêu cầu đơn giản. Để biết danh sách đầy đủ các phương thức, hãy xem tài liệu tham khảo.

Liệt kê "Danh bạ khác" của người dùng

Để nhận danh sách những người trong "Danh bạ khác" của người dùng, hãy sử dụng mã sau:

Giao thức

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();

Liệt kê "Danh bạ khác" của người dùng đã thay đổi

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
  }
}

Thông tin chi tiết khác về hoạt động đồng bộ hoá tại ListOtherContacts.

Sao chép "Người liên hệ khác" vào nhóm "MyDanh bạ"

Để sao chép "Người liên hệ khác" vào nhóm "myDanh bạ", hãy sử dụng mã sau:

Giao thức

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();

Tìm kiếm trong "Danh bạ khác" của người dùng

Để tìm kiếm tất cả "Danh bạ khác" của người dùng, hãy sử dụng mã sau:

Giao thức

// 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();