无法使用Microsoft Graph API C#代码获取部门名称、经理等

无法使用Microsoft Graph API C#代码获取部门名称、经理等,c#,azure-active-directory,microsoft-graph-api,C#,Azure Active Directory,Microsoft Graph Api,我正在使用以下代码: using Microsoft.Graph; using Microsoft.Identity.Client; using System; namespace MSGraphAPI { class Program { private static string clientId = "XXX"; private static string tenan

我正在使用以下代码:

  using Microsoft.Graph;
    using Microsoft.Identity.Client;
    using System;

    namespace MSGraphAPI
    {
        class Program
        {


            private static string clientId = "XXX";


            private static string tenantID = "XXXX";


            private static string objectId = "XXXX";


            private static string clientSecret = "XXXXX";

            static async System.Threading.Tasks.Task Main(string[] args)
            {

                int Flag = 0;
                var tenantId = "XXX.onmicrosoft.com";

                var clientId = "XXXXX";

                var clientSecret = "XXXXX"; // Or some other secure place.

                var scopes = new string[] { "https://graph.microsoft.com/.default" };

                // Configure the MSAL client as a confidential client
                var confidentialClient = ConfidentialClientApplicationBuilder
                    .Create(clientId)
                    .WithAuthority($"https://login.microsoftonline.com/XXX.onmicrosoft.com/v2.0")
                    .WithClientSecret(clientSecret)
                    .Build();


                GraphServiceClient graphServiceClient =
                    new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {

            // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
            var authResult = await confidentialClient
                .AcquireTokenForClient(scopes)
                .ExecuteAsync();

            // Add the access token in the Authorization header of the API request.
            requestMessage.Headers.Authorization =
                new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                    })
                    );


                var users = await graphServiceClient.Users.Request().GetAsync();


                do
                {
                            foreach (User user in users)
                            {    

                            Console.WriteLine(user.DisplayName);
                            Console.WriteLine(user.MobilePhone);
                            Console.WriteLine(user.BusinessPhones);
                            Console.WriteLine(user.DirectReports);
                            Console.WriteLine(user.Manager);    
                            var directoryObject = await graphClient.Users[user.userPrincipalName].Manager
    .Request()
    .GetAsync();

  foreach (User user1 in directoryObject )
                        {
// unable to print using user1.displayName

                        }
                            }
                        }
                        while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);

                        Console.WriteLine("------");

            }
        }
    }
但是,我无法获取directReports或任何用户,我可以获取DisplayName等。 Azure应用程序注册门户中已提供对user.Read等的权限。 我也尝试过user.manager打印详细信息,但无法打印值。我还附上了应用程序访问的截图

var directoryObject = await graphClient.Users["{id|userPrincipalName}"].Manager
    .Request()
    .GetAsync();
directoryObject在调试模式下显示displayName,但无法将displayName打印为directoryObject.displayName


请帮助,因为我无法获取所需的详细信息。

首先,您需要确保用户有一个管理员。您可以使用来检查它

然后,您需要调用
graphClient.Users[{id | userPrincipalName}].Manager
来获取管理器

var user =  graphServiceClient.Users["user@XX.onmicrosoft.com"].Request().GetAsync().Result;
            var manager = (User) graphServiceClient.Users["user@XX.onmicrosoft.com"].Manager.Request().GetAsync().Result;
            Console.WriteLine("user_displayName: "+user.DisplayName);
            Console.WriteLine("manager_displayName: "+manager.DisplayName);

请尝试将
directoryObject
转换为
User
。查看答案,如果有帮助,您可以将其作为答案接受(单击答案旁边的复选标记,将其从灰色切换为填充)。这可能对其他社区成员有益。非常感谢。