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

非 Y 不嫁゛ 提交于 2019-12-05 15:12:16

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.

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