Baxorr
Baxorr

Reputation: 318

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://learn.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://learn.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 ad app permissions

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)

Upvotes: -1

Views: 791

Answers (1)

Tiny Wang
Tiny Wang

Reputation: 15906

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

enter image description here enter image description here

So in this scenario, request https://graph.microsoft.com/v1.0/places equals to 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. 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();

enter image description here

Upvotes: 6

Related Questions