C#中的Microsoft Graph API代码仅显示有限数量的用户 [英] Microsoft Graph api code in C# displays only limited number of users

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

问题描述

我在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).在

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代码仅显示有限数量的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆