读取网域联系人和个人资料

完成准备使用 People API 中的步骤后,您就可以读取目录联系人和个人资料了。

以下代码示例演示了如何发送一些简单的请求。如需查看完整的方法列表,请参阅参考文档

列出目录人员

如需获取用户网域目录中的联系人和个人资料列表,请使用以下代码:

协议

GET /v1/people:listDirectoryPeople?sources=DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT&sources=DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com

Java

List<String> sources = new ArrayList<>();
sources.add("DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT");
sources.add("DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE");

ListDirectoryPeopleResponse response = peopleService.people().listDirectoryPeople()
    .setSources(sources)
    .setReadMask("metadata,names,emailAddresses")
    .execute();

List<Person> people = response.getPeople();

列出目录中已更改的人员

Java

// Initial request
List<String> sources = new ArrayList<>();
sources.add("DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT");
sources.add("DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE");

ListDirectoryPeopleResponse fullSyncResponse = peopleService.people().listDirectoryPeople()
    .setSources(sources)
    .setReadMask("metadata,names,emailAddresses")
    .setRequestSyncToken(true)
    .execute();
// Fetch all the pages
while (fullSyncResponse.getNextPageToken() != null) {
  fullSyncResponse = peopleService.people().listDirectoryPeople()
      .setSources(sources)
      .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 {
  ListDirectoryPeopleResponse incrementalSyncResponse = peopleService.people().listDirectoryPeople()
      .setSources(sources)
      .setReadMask("metadata,names,emailAddresses")
      .setSyncToken(fullSyncResponse.getNextSyncToken())
      .execute();
  for (Person person : incrementalSyncResponse.getDirectoryPeople()) {
    handlePerson(person);
  }
  
  // Fetch all the pages
  while (incrementalSyncResponse.getNextPageToken() != null) {
    incrementalSyncResponse = peopleService.people().listDirectoryPeople
        .setSources(sources)
        .setReadMask("metadata,names,emailAddresses")
        .setSyncToken(fullSyncResponse.getNextSyncToken())
        .setPageToken(incrementalSyncResponse.getNextPageToken())
        .execute();
    for (Person person : incrementalSyncResponse.getDirectoryPeople()) {
      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
  }
}

如需详细了解同步行为,请参阅 ListDirectory

搜索目录中的人员

如需获取与前缀查询匹配的用户网域目录中的联系人和个人资料列表,请使用以下代码:

协议

POST /v1/people:searchDirectoryPeople?query=John&sources=DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT&sources=DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com

Java

List sources = new ArrayList<>();
sources.add("DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT");
sources.add("DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE");

SearchDirectoryPeopleResponse response = peopleService.people().searchDirectoryPeople()
    .setQuery("John")
    .setSources(sources)
    .setReadMask("metadata,names,emailAddresses")
    .execute();

List<Person> people = response.getPeople();