C# 中的 Microsoft Graph api 代码仅显示有限数量的用

时间:2023-05-07
本文介绍了C# 中的 Microsoft Graph api 代码仅显示有限数量的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在 Microsoft Graph Api 代码下运行:

I am running below Microsoft Graph Api code:

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace AADConsole2
{
    class Program
    {

        private const string aadInstance = "https://login.microsoftonline.com/{0}";
        //  private const string ResourceUrl = "https://graph.windows.net";

        private const string resource = "https://graph.microsoft.com";
        private const string GraphServiceObjectId = "XXX";
        private const string TenantId = "XXX";
        private const string tenant = "XXXX.onmicrosoft.com";
        private const string ClientId = "XXX";
        private static string appKey= "XXXX";
        static string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, aadInstance, tenant);

        private static HttpClient httpclient = new HttpClient();
        private static AuthenticationContext context = null;
        private static ClientCredential credential = null;


        static void Main(string[] args)
        {

            context = new AuthenticationContext(authority);
            credential = new ClientCredential(ClientId, appKey);
            Task<string> token = GetToken();
            token.Wait();
            Console.WriteLine(token.Result);
            Task<string> users = GetUsers(token.Result);
            users.Wait();
            Console.WriteLine(users.Result);
            Console.ReadLine();
        }

        private static async Task<string> GetUsers(string result) {
            //throw new NotImplementedException();
            string users = null;
            var uri = "https://graph.microsoft.com/v1.0/users";
            httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result);
            var getResult = await httpclient.GetAsync(uri);

            if(getResult.Content != null)
            {
                users = await getResult.Content.ReadAsStringAsync();

            }
            return users;

        }


        private static async Task<string> GetToken()
        {
            AuthenticationResult result = null;
            string token = null;
            result = await context.AcquireTokenAsync(resource, credential);
            token = result.AccessToken;
            return token;



        }
    }
}

我在控制台上打印了用户详细信息的结果,但只打印了有限数量的用户.即只有其名称以字母a"开头.并且还缺少一些用户详细信息.如何获取所有用户详细信息.我在这段代码中缺少一些 api 吗?谢谢.

I am getting the results of user detail printed on console ,but only limited number of users are printed. i.e only whose name starts with letter 'a'. And also some user details are missing. How to get all user details .Am i missing some api in this code? Thanks.

推荐答案

大多数 Microsoft Graph 端点返回分页结果集.您的初始请求仅返回第一页数据.要检索下一页,请遵循 @odata.nextLink 属性中提供的 URI.每个后续页面将返回下一页的 @odata.nextLink 直到您的最后一页数据(由结果中缺少 @odata.nextLink 表示).在 在您的应用中分页 Microsoft Graph 数据.

Most Microsoft Graph endpoints return paged result sets. Your initial request only returns the first page of data. To retrieve the next page, you follow the URI provided in the @odata.nextLink property. Each subsequent page will return the next page's @odata.nextLink until you the last page of data (denoted by the lack of a @odata.nextLink in the result). There is a step-by-step walkthrough of how this works at Paging Microsoft Graph data in your app.

我可以在这里给你的最重要的一条提示是不要使用 $top 来强制它返回大页数据.这是一种调用 API 效率极低的方法,不可避免地会导致网络错误和请求限制.它也没有消除处理分页的需要,因为即使 $top=999(最大值)仍然可以返回多个页面.

The single most important tip I can give you here is to not use $top to force it to return large pages of data. This is an extremely inefficient method for calling the API and inevitably leads to network errors and request throttling. It also doesn't eliminate the need to handle paging since even $top=999 (the maximum) can still return multiple pages.

实现分页,保持页面较小,并在返回每个页面后处理结果,然后再转到下一页.这将确保您捕获所有数据允许您的应用程序在处理过程中遇到任何错误时从中断的地方继续.

Implement paging, keep your page sizes small, and process the results after each page is returned before moving on to the next page. This will ensure you capture all of the data and allow your application to pick up where it left off should it encounter any errors during processing.

这篇关于C# 中的 Microsoft Graph api 代码仅显示有限数量的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:ASP.NET Core 2.0 AzureAD 身份验证不起作用 下一篇:通过 microsoft graph 从 azure ad 获取组成员

相关文章

最新文章