简体   繁体   English

从 C# 调用图形 API 时出现 NotAcceptable 错误

[英]NotAcceptable error when calling graph API from C#

Hey guys i'm trying to get a list of all rooms in a tenant but i'm receiving an error I see no solution to.嘿伙计们,我正在尝试获取租户中所有房间的列表,但我收到一个错误,我看不到解决方案。 My graph service client appears to be correct since I can get a list of all users without issues, but getting all the rooms fails with an unknown error.我的图形服务客户端似乎是正确的,因为我可以毫无问题地获取所有用户的列表,但是获取所有房间失败并出现未知错误。

What i'm trying to replicate: https://docs.microsoft.com/en-us/graph/api/place-list?view=graph-rest-1.0&tabs=csharp#request我要复制的内容: https://docs.microsoft.com/en-us/graph/api/place-list?view=graph-rest-1.0&tabs=csharp#request

Creating graph client and attempting to get roomslist from places创建图形客户端并尝试从地点获取房间列表

public GraphServiceClient CreateGraphClient()
{
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

    // https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);

    return new GraphServiceClient(clientSecretCredential, scopes);
}

public async Task<List<string>> GetRoomsAsync()
{
    var rooms = await graphClient.Places.Request().GetAsync();

    var roomsList = new List<string>();

    foreach (var room in rooms)
    {
        Console.WriteLine(room.DisplayName);
        roomsList.Add(room.DisplayName);
    }
    return roomsList;
}

The same thing works when trying to get users.在尝试获取用户时,同样的事情也有效。

    public async Task<List<string>> GetUsersAsync()
    {
        var users = await graphClient.Users.Request().GetAsync();
        var user_list = new List<string>();

        foreach (var user in users)
        {
            user_list.Add(user.DisplayName);
        }

        return user_list;
    }

My azure ad permissions我的 azure 广告权限

Azure 广告应用权限

Error code:错误代码:

      An unhandled exception has occurred while executing the request.
      Status Code: NotAcceptable
      Microsoft.Graph.ServiceException: Code: UnknownError
      Inner error:
        AdditionalData:
        date: 2022-09-11T12:06:02
        request-id: 1d5d8c56-235c-452d-aa2c-9d71cbf2d7a9
        client-request-id: 1d5d8c56-235c-452d-aa2c-9d71cbf2d7a9
      ClientRequestId: 1d5d8c56-235c-452d-aa2c-9d71cbf2d7a9

         at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
         at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
         at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
         at Microsoft.Graph.GraphServicePlacesCollectionRequest.GetAsync(CancellationToken cancellationToken)
         at PicoWebAPI.Controllers.MeetingsController.Get() in C:\Users\Blue\source\repos\MeetingRoomBooking\PicoWebAPI\Controllers\MeetingsController.cs:line 25
         at lambda_method5(Closure , Object )
         at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
         at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Let's see the api response via http request , and we can also see the graph api return the same UnknownError as graph SDK: Let's see the api response via http request , and we can also see the graph api return the same UnknownError as graph SDK:

在此处输入图像描述 在此处输入图像描述

So in this scenario, request https://graph.microsoft.com/v1.0/places equals to SDK await graphClient.Places.Request().GetAsync();所以在这种情况下,请求https://graph.microsoft.com/v1.0/places等于 SDK await graphClient.Places.Request().GetAsync(); which should met this error.应该遇到这个错误。 And this is because Graph SDK does not currently support filtering by derived types which is a known issue.这是因为 Graph SDK 目前不支持按派生类型进行过滤,这是一个已知问题。 A similar issue here.这里有一个类似的问题

And this is the workaround and it worked for me:这是解决方法,它对我有用:

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var roomUrl = graphClient.Places.AppendSegmentToRequestUrl("microsoft.graph.room");
var placesRequest = await new GraphServicePlacesCollectionRequest(roomUrl, graphClient, null).GetAsync();

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM