我正在尝试使用具有azure 门户中的和权限GraphServiceClient
的注册应用程序来读取单个计划的所有计划任务。Group.Read.All
Group.ReadWrite.All
我已经尝试使用Microsoft.Graph
和Microsoft.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);
}