0

我正在尝试使用具有azure 门户中的和权限GraphServiceClient的注册应用程序来读取单个计划的所有计划任务。Group.Read.AllGroup.ReadWrite.All

我已经尝试使用Microsoft.GraphMicrosoft.Graph.Beta包,但在调用任何规划器 api 端点时我总是得到一个 InternalServerError 。

GraphServiceClient使用以下代码设置

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
             .Create("ClientId")
             .WithTenantId("TenantId")
             .WithClientSecret("ClientSecret")
             .Build();

ClientCredentialProvider clientCredentialProvider = new ClientCredentialProvider(confidentialClientApplication);

GraphServiceClient graphClient = new GraphServiceClient(clientCredentialProvider);

工作正常。调用组api

try
{
    Task<IGraphServiceGroupsCollectionPage> groups = graphClient.Groups
        .Request()
        .GetAsync();
    groups.Wait();

    foreach (var item in groups.Result)
    {
        Console.WriteLine(item.DisplayName);
    }
}
catch (Exception e)
{
    Console.WriteLine(e);
}

也有效。但是调用planner API

try
{
    Task<PlannerPlan> plan = graphClient.Planner.Plans["PlanId"]
        .Request()
        .GetAsync();
    plan.Wait();
    Console.WriteLine(plan.Result.Title);
}
catch (Exception e)
{
    Console.WriteLine(e);
}

在“/taskApi”应用程序中引发预期服务器错误。

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

[NullReferenceException:对象引用未设置为对象的实例。]
Microsoft.Office.Tasks.Service.S2SProxies.FederatedGraph.FederatedGraphService.DeserializeFederatedGraphObject(String json) +35
Microsoft.Office.Tasks.Service.S2SProxies.FederatedGraph.d__51。 MoveNext() +1385 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) +60
Microsoft.Office.Tasks.Service.S2SProxies.FederatedGraph.d__47.MoveNext() +509

[FederatedGraphProxyException:获取图形对象时出错]
Microsoft.Office.Tasks.Service.CorrelationSafeAsync.ExecuteSynchronously(Func`2 asyncFunction,CancellationToken cancelToken)+294
Microsoft.Office.Tasks.Service.UserAccountManager.AadUserAccountManager.GetExternalUserInfoInternal(IdentityClaim 声明) +520

[InvalidOperationException:检索图形对象时出现意外错误] Microsoft.Office.Tasks.Service.UserAccountManager.AadUserAccountManager.GetExternalUserInfoInternal(IdentityClaim 声明) +1288
Microsoft.Office.Tasks.Service.UserAccountManager.AadUserAccountManager.EnsureUser(String displayName, Boolean isLogin, CancellationToken cancelToken , IdentityClaim[] 声明) +1013
Microsoft.Office.Tasks.Service.Authentication.AuthenticationModuleBase.EnsureUserAndSetCurrentUserProvider(HttpContextBase httpContext, IUserAuthentication currentUser) +936
Microsoft.Office.Tasks.Service.Authentication.AuthenticationModuleBase.HandleAuthenticatedUser(HttpContextBase httpContextBase, IUserAuthentication currentUser) +168
Microsoft.Office.Tasks.Service.Authentication.AuthenticationModuleBase.AuthorizeRequest(Object sender, EventArgs e) +1867
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +223 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +213 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +91

使用我的 microsoft 帐户可以工作,尽管我必须更改身份验证提供程序。

我的代码中缺少什么?

编辑:工作代码

感谢@Jim Xu,我的代码可以正常工作了。对于遇到相同问题的任何人,这里都是工作示例。我必须将客户端密码添加到所有请求的标头中,但它仍然有效。

IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("CLientId")
.WithTenantId("TenantId")
.Build();
String[] scopes = new String[] { "Group.Read.All", "Group.ReadWrite.All" };
Func<DeviceCodeResult, Task> deviceCodeReadyCallback = async dcr => await Console.Out.WriteLineAsync(dcr.Message);

DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, scopes, deviceCodeReadyCallback);

GraphServiceClient graphClient = new GraphServiceClient(authProvider);
IGraphServiceGroupsCollectionRequest groupRequest = graphClient.Groups.Request();
groupRequest.Headers.Add(new HeaderOption("client_secret", "ClientSecrect"));

Task<IGraphServiceGroupsCollectionPage> groups = groupRequest.GetAsync();
groups.Wait();

foreach (var item in groups.Result)
{
    Console.WriteLine(item.DisplayName);
}
4

1 回答 1

1

根据您的代码,您使用客户端凭据流来调用 Microsoft 计划 API。换句话说,您使用应用程序权限来调用 Microsoft Graph Plan API。这是错误的。根据官方文档,我们只需要使用 Delegated 权限来调用 API。所以你会得到错误。 在此处输入图像描述

关于如何用SDk调用api,请参考以下代码(我在控制台应用中测试)

  1. 配置 Azure AD 应用程序

    一个。打开allowPublicClient设置 在此处输入图像描述

    湾。更新 API 权限。请添加需要委托的权限:Group.Read.AllGroup.ReadWrite.All

  2. 代码

  string clientId = "";
            string tenantId = "";
            IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
            .Create(clientId)
            .WithTenantId(tenantId)
            .Build();
            var scopes = new string[] { "Group.Read.All", "Group.ReadWrite.All" };
            Func<DeviceCodeResult, Task> deviceCodeReadyCallback = async dcr => await Console.Out.WriteLineAsync(dcr.Message);

            DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, scopes, deviceCodeReadyCallback);
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);
            Task<PlannerPlan> plan = graphClient.Planner.Plans["Lc1LT2z1aEqnACVeA8KtIGUABIeE"]
                .Request()
                .GetAsync();
             plan.Wait();
             Console.WriteLine("The plan tiltel : " + plan.Result.Title);
            Console.ReadLine();

在此处输入图像描述

于 2020-05-01T03:12:58.837 回答