GraphServiceClient request returns “method not allowed” error, but it works then calling the API with token

Vruyr Manukyan 96 Reputation points
2021-06-16T13:25:56.15+00:00

I'm trying to register new app using GraphServiceClient, but it fails

app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
      .WithCertificate(certificate)
      .WithAuthority(new Uri(config.Authority))
      .Build();
var authProvider = new ClientCredentialProvider(app, $"{config.ApiUrl}.default");
var client = new GraphServiceClient(config.ApiUrl, authProvider)

var application = new Application
{
     DisplayName = "test"
};
Application created = await client.Applications.Request().AddAsync(application);

this last call throws an exception - Method not allowed.
But I'm able to register an application by calling API using JWT token, so I believe that config properties are correct.

string[] scopes = new string[] { $"{config.ApiUrl}.default" };
AuthenticationResult result = await app.AcquireTokenForClient(scopes)
                        .WithSendX5C(true)
                        .ExecuteAsync();
...
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await httpClient.PostAsync($"{config.ApiUrl}v1.0/applications", content);

What do I miss for using GraphServiceClient?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,775 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vruyr Manukyan 96 Reputation points
    2021-06-16T15:21:41.213+00:00

    For creating GraphServiceClient object I needed also to append v1.0 to url.

    var client = new GraphServiceClient(config.ApiUrl + "v1.0", authProvider) // https://graph.microsoft.com/v1.0
    

    Now it works.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.