简体   繁体   中英

members.list() in Google Admin SDK Directory API (Java)

I have a piece of code(in Java) to list all members of a group on a personal Google Apps Domain. This uses the google Directory API.

Here is the snippet:

public static void listMembers(String groupKey,Directory service) throws IOException {
        Members res = service.members().list(groupKey).execute();

        List<Member> members = res.getMembers();
        int count = 0;
        if (members == null || members.size() == 0) {
            System.out.println();
            System.out.println("No members found.");
        } else {
            System.out.println();
            System.out.println("Members of "+groupKey);
            for (Member member : members) {
                count++;
                System.out.println(member.getEmail());
            }
            System.out.println(count);
        }
    }

This works fine, but for any group, not more than exactly 200 members are listed, though a group actually has more users. I tried to search for the limit on the members.list() function that I am using, but couldn't find it on the Google Documentation for the Directory API. Is there any such limit? If yes, can I somehow list all users?

Take a look at the maxResults and pageToken attributes on members.list() . The page doesn't specify but I believe 200 is both the maxResults default value and maximum. Your app needs to check for the existence of the pageToken attribute in the results. If it's set, you have at least one more page of results to grab. Keep looping through results until pageToken is not set.

I have modified the code to use the pageToken attribute , as below:

public static void listMembers(String groupKey,Directory service) throws IOException {

        Directory.Members.List res = service.members().list(groupKey);
        Members mbrs;
        List<Member> members ;
        int count = 0;
        String pageToken;
        do{
                mbrs = res.execute();
                members = mbrs.getMembers();
                System.out.println();
                System.out.println("Members of "+groupKey);
                for (Member member : members) {
                    count++;
                    System.out.println(member.getEmail());
                }
                pageToken = mbrs.getNextPageToken();
                //System.out.println(res.getPageToken());   //The first pageToken of any Directory.Members.List is null.
                res.setPageToken(pageToken);
                System.out.println(count);
        }while(pageToken!=null);
    }

I would like to add, the first pageToken of any Directory.Members.List is null. One can verify this, by changing the while loop's condition from pageToken!=null to true . This change will list all the members over and over again repeatedly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM