Share via

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 Security | Microsoft Graph
0 comments No comments

Answer accepted by question author

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.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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