Alan Kişilerini ve Profillerini Okuma

People API'yi Kullanmaya Hazırlanma başlıklı makaledeki adımları tamamladıktan sonra dizin kişilerini ve profillerini okumaya hazır olursunuz.

Aşağıdaki kod örnekleri, birkaç basit isteğin nasıl gönderileceğini göstermektedir. Yöntemlerin tam listesi için referans dokümanlarını inceleyin.

Dizindeki kişileri listeleme

Kullanıcı alan dizininde bulunan kişilerin ve profillerin listesini almak için aşağıdaki kodu kullanın:

Protokol

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

Değişen dizindeki kişileri listeleme

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

Senkronizasyon davranışı hakkında daha fazla bilgiyi ListDirectory işlevinde bulabilirsiniz.

Dizindeki kullanıcıları arayın

Kullanıcı alan dizininde, önek sorgusuyla eşleşen kişilerin ve profillerin listesini almak için aşağıdaki kodu kullanın:

Protokol

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