簡體   English   中英

Microsoft Graph API 分頁無法從 Azure AD 獲取所有用戶

[英]Microsoft Graph API Pagination is not working for getting all users from Azure AD

我們正在使用Microsoft Graph API Beta 版使用以下代碼從 Azure AD 檢索所有用戶。 API 在響應中僅返回 100 個用戶,為了使用分頁響應,我們嘗試了NextPageRequest屬性。 但它總是為NextPageRequest屬性返回null 因此,它永遠不會進入 while 循環以檢索用戶的 rest。

測試版 SDK 版本:4.0.1.0

代碼:

                List<User> usersList = new List<User>();
                IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().GetAsync();

                // Add the first page of results to the user list
                usersList.AddRange(users.CurrentPage);

                // Fetch each page and add those results to the list
                while (users.NextPageRequest != null)
                {
                    users = await users.NextPageRequest.GetAsync();
                    usersList.AddRange(users.CurrentPage);
                }

                log.Info("Users count: " + usersList.Count.ToString());
                return usersList;

我遵循的參考鏈接:

對此的任何幫助將不勝感激!

下面的代碼對我來說非常好。

public static async Task<List<User>> getUsers()
        {
            List<User> usersList = new List<User>();
            graphClient.BaseUrl = "https://graph.microsoft.com/beta";
            IGraphServiceUsersCollectionPage users = await graphClient.Users
                .Request()
                .GetAsync();

            usersList.AddRange(users.CurrentPage);

            while (users.NextPageRequest != null)
            {
                users = await users.NextPageRequest.GetAsync();
                usersList.AddRange(users.CurrentPage);
            }
            return usersList;
        }

檢查 Azure Active Directory 用戶刀片中的用戶,並查看其中存在多少用戶。 此外,您還可以通過簡單地使用 $top 查詢參數擴展代碼來測試用戶數量是否超過 100,該參數為每個請求提供 998 條記錄,如下所示。

IGraphServiceUsersCollectionPage users = await graphClient.Users
                    .Request()
                    .Top(998)
                    .GetAsync();

您還可以在Graph Explorer中測試 Graph API 調用。

編輯:

經過長時間的研究,我發現它是 Microsoft Graph Beta SDK 中的一個錯誤,因為它總是在NextPageRequest中發送 null 。 但這里有趣的是,它在AdditionalData屬性中發送odata.nextLink 因此,如果您使用的是 Graph Beat SDK,請使用以下代碼。

public static async Task<List<User>> getUsers()
        {
            List<User> usersList = new List<User>();
            IGraphServiceUsersCollectionPage users = await graphClient.Users
                .Request()
                .GetAsync();

            usersList.AddRange(users.CurrentPage);
            try
            {
                while (users.AdditionalData["@odata.nextLink"].ToString() != null)
                {
                    users.InitializeNextPageRequest(graphClient, users.AdditionalData["@odata.nextLink"].ToString());
                    users = await users.NextPageRequest.GetAsync();
                    usersList.AddRange(users.CurrentPage);
                }
            }
            catch(Exception e)
            {
                
            }
            return usersList;
        }

注意: Microsoft 不建議在生產中使用其Graph Beta 版本,因為它們可能會發生變化。

我正在添加以下代碼,以便它可以幫助 java 開發人員解決此問題。

它不是對該線程的回答,而是針對 java 用戶的。

graphServiceClient = graphConnection.getGraphConnection();
List<User> users = new ArrayList<>();
UserCollectionPage usersPage = graphServiceClient.users().buildRequest().select(COLUMN_NAMES)
    .expand("manager($select=id,givenName,mail,userPrincipalName)").get();

while (usersPage != null) {
  final List<User> currentPageUsers = usersPage.getCurrentPage();
  users.addAll(currentPageUsers);
  UserCollectionRequestBuilder nextPage = usersPage.getNextPage();
  if (nextPage == null) {
    break;
  } else {
    usersPage = nextPage.buildRequest().get();
  }
}

return users;

這是工作代碼。 我已經測試了 200 個用戶。 允許的最大頁面大小為 100。我嘗試了 top(999),它每頁最多只能獲取 100。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM