問題描述
我在 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模板網!