调用Graph API获取B2C目录用户仅返回100条且userType为空如何解决
问题原因说明
- Graph API 所有列表查询接口默认开启分页限制,单页最多返回100条数据,
getCurrentPage()仅返回当前页数据,本身就不包含全量结果 userType为空是因为 Graph API 默认仅返回用户的基础属性集,userType属于需要主动指定才会返回的扩展属性
解决方案
方案1:单页拉取全量(适配你当前800+用户的场景)
Graph 单页查询最多支持返回999条数据,你可以直接通过top参数指定单次拉取数量,同时用select指定要返回的userType字段,代码如下:
UserCollectionPage users = graphClient.users() .buildRequest() .select("id,displayName,userType,mail,userPrincipalName") // 按需添加你需要的属性 .top(999) .get(); List<User> userList = users.getCurrentPage();
此时返回的userList就包含全量800+用户,且userType字段会正常返回数值。
方案2:分页遍历拉取(适配用户量超过1000的场景)
如果后续目录用户量超过999,可通过遍历所有分页的方式拉取全量数据,代码示例:
List<User> allUserList = new ArrayList<>(); UserCollectionPage usersPage = graphClient.users() .buildRequest() .select("id,displayName,userType,mail,userPrincipalName") .top(999) .get(); // 遍历所有分页 while (usersPage != null) { allUserList.addAll(usersPage.getCurrentPage()); UserCollectionRequestBuilder nextPage = usersPage.getNextPage(); if (nextPage == null) { break; } usersPage = nextPage.buildRequest().get(); }
最终allUserList即为全量用户数据。
直接获取JSON格式结果
使用你已完成授权的Graph Client发送GET请求到如下路径,就能拿到JSON格式的全量返回:GET https://graph.microsoft.com/v1.0/users?$top=999&$select=id,displayName,userType
内容的提问来源于stack exchange,提问作者Sharad esdee Dutta
相关产品推荐
相关产品推荐

