อ่าน คัดลอก และค้นหา "รายชื่อติดต่ออื่นๆ"

หลังจากทำตามขั้นตอนในเตรียมพร้อมใช้งาน People API แล้ว คุณก็พร้อมที่จะอ่าน คัดลอก และค้นหาข้อมูล "รายชื่อติดต่ออื่นๆ"

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีส่งคําของ่ายๆ 2-3 รายการ ดูรายการวิธีการทั้งหมดได้ในเอกสารอ้างอิง

แสดง "รายชื่อติดต่ออื่นๆ" ของผู้ใช้

หากต้องการดูรายชื่อบุคคลใน "รายชื่อติดต่ออื่นๆ" ของผู้ใช้ ให้ใช้รหัสต่อไปนี้

โปรโตคอลJava
GET /v1/otherContacts?readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com
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" ให้ใช้รหัสต่อไปนี้

โปรโตคอลJava
POST /v1/resource_name:copyOtherContactToMyContactsGroup?copyMask=names,emailAddresses,phoneNumbers HTTP/1.1
Host: people.googleapis.com
Person copiedContact = peopleService
   
.otherContacts()
   
.copyOtherContactToMyContactsGroup(
       
"resource_name",
       
new CopyOtherContactToMyContactsGroupRequest()
           
.setCopyMask("names,emailAddresses,phoneNumbers"))
   
.execute();

ค้นหา "รายชื่อติดต่ออื่นๆ" ของผู้ใช้

หากต้องการค้นหา "รายชื่อติดต่ออื่นๆ" ทั้งหมดของผู้ใช้ ให้ใช้รหัสต่อไปนี้

โปรโตคอลJava
// 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
// 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();