在C#中使用Graph API获取团队组织分层数据的步骤如下:
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string clientId = "YOUR_CLIENT_ID";
string clientSecret = "YOUR_CLIENT_SECRET";
string tenantId = "YOUR_TENANT_ID";
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.Build();
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
AuthenticationResult authenticationResult = await confidentialClientApplication
.AcquireTokenForClient(scopes)
.ExecuteAsync();
GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
return Task.CompletedTask;
}));
var organization = await graphServiceClient
.Me
.Organization
.Request()
.GetAsync();
Console.WriteLine($"Organization: {organization.DisplayName}");
var users = await graphServiceClient
.Users
.Request()
.GetAsync();
foreach (var user in users)
{
Console.WriteLine($"User: {user.DisplayName}");
}
}
}
请注意,上述代码中的YOUR_CLIENT_ID
,YOUR_CLIENT_SECRET
和YOUR_TENANT_ID
需要替换为你在Azure门户中创建的应用程序注册的实际值。
这段代码使用Microsoft.Identity.Client库进行身份验证,并使用GraphServiceClient类与Microsoft Graph API进行交互。它首先获取访问令牌,然后使用该令牌从Graph API中检索组织信息和用户信息。
这是一个基本的示例,你可以根据自己的需求进一步扩展和定制代码。有关Graph API的更多信息和其他操作,请参考腾讯云的Microsoft Graph API文档:链接地址
领取专属 10元无门槛券
手把手带您无忧上云