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