Not quite sure how to solve this error because it isn't specific when I catch the error message
Microsoft.Graph.Models.ODataErrors.ODataError: Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(HttpResponseMessage response, Dictionary`2 errorMapping, Activity activityForAttributes)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory`1 factory, Dictionary`2 errorMapping, CancellationToken cancellationToken)
at Microsoft.Graph.Users.Item.UserItemRequestBuilder.PatchAsync(User body, Action`1 requestConfiguration, CancellationToken cancellationToken)
It specifically errors out on this line:
await graphClient.Users[$"{id}"].PatchAsync(user);
For more context, I am trying to update a users onPremisesExtensionAttributes
In another method elsewhere I create them like this
var user = new User
{
OnPremisesExtensionAttributes = new OnPremisesExtensionAttributes
{
ExtensionAttribute1 = "123"
ExtensionAttribute2 = "abc"
}
};
Everything is correct according to the Microsoft documentation. I am not quite sure how to proceed with this one, I have spent 2 hours reading about it but nothing has fixed it. It was working up until I was tasked to revisit this project and update all dependencies. According to documentation though, this is how you are supposed to patch a user with Graph SDK v5
Any help is greatly appreciated
When I ran below code by including try-catch method, I got same error with Insufficient privileges
in console like this:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "appId";
var tenantId = "tenantId";
var clientSecret = "secret";
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = new User
{
OnPremisesExtensionAttributes = new OnPremisesExtensionAttributes
{
ExtensionAttribute1 = "123",
ExtensionAttribute2 = "abc"
}
};
try
{
await graphClient.Users["b3235d2e-19bc-4b2b-a8ef-xxxxxxx"].PatchAsync(user);
Console.WriteLine("Extension attributes updated successfully");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
Response:
To resolve the error, I added User.ReadWrite.All
permission of Application type in application by granting admin consent:
When I ran the same code again now, I got the response successfully like this:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "appId";
var tenantId = "tenantId";
var clientSecret = "secret";
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = new User
{
OnPremisesExtensionAttributes = new OnPremisesExtensionAttributes
{
ExtensionAttribute1 = "123",
ExtensionAttribute2 = "abc"
}
};
try
{
await graphClient.Users["b3235d2e-19bc-4b2b-a8ef-xxxxxxx"].PatchAsync(user);
Console.WriteLine("Extension attributes updated successfully");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
throw;
}
Response:
To confirm that, I checked the same in Portal where user's extension attributes are updated like below: