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

只谈情不闲聊 提交于 2019-12-07 11:19:22

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/31243902/members-list-in-google-admin-sdk-directory-api-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!