【发布时间】:2020-05-04 14:45:30
【问题描述】:
我正在尝试使用在 azure 门户中具有 Group.Read.All 和 Group.ReadWrite.All 权限的注册应用程序来读取具有 GraphServiceClient 的单个计划的所有计划任务。
我尝试过使用 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”应用程序中引发服务器错误。
在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
[NullReferenceException: 对象引用未设置为 对象。]
Microsoft.Office.Tasks.Service.S2SProxies.FederatedGraph.FederatedGraphService.DeserializeFederatedGraphObject(字符串 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(字符串 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(对象 发件人,EventArgs e) +1867
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +223 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep 步骤) +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);
}
【问题讨论】:
-
使用像wireshark或fiddler这样的嗅探器。错误 500 表示您发送的请求不正确。将工作 api 中的第一个请求标头与非工作 c# 应用程序进行比较。然后修改 c# 使其看起来像工作 api。
-
请求是一样的,除了请求的url不同。而且我认为错误要么是我们在 azure 门户中的配置,要么是 api 本身,虽然不是很合理。
-
这两个 URL 是否处理相同类型的请求(相同的 API,相同的版本)?你是如何在服务器上安装 API 的?服务器上的默认值可能不同。例如,一个可能正在处理 http 1.0,另一个可能正在处理 http 1.1。 Net 的版本可能不同,因此您可能需要发布和安装 API。 API 版本可能不同。
-
我正在使用 microsoft graph api。所有请求都转到 grap.microsoft.com。我刚刚在我们的 azure AD 中添加了应用注册。
标签: c# azure microsoft-graph-api microsoft-planner