I’m trying to make a call to Microsoft’s Graph api to update device ownership types for the company I’m working for. We have over 4,000 devices so this is something that needs to be automated. I’m writing a .NET app that uses a GraphServiceClient to connect to the api and make requests.
For some reason I keep receiving the following error as soon as I make the call, and I’m struggling to understand why?? (Device id and other information has been replaced with x’s)
Microsoft.Graph.Models.ODataErrors.ODataError: {
"_version": 3,
"Message": "An error has occurred - Operation ID (for customer support): 00000000-0000-0000-0000-000000000000 - Activity ID: xxxx-xxxx-xxxx-xxxx - Url: https://fef.msua06.manage.microsoft.com/DeviceFE/StatelessDeviceFEService/deviceManagement/managedDevices('XXXXXXXXXX')?api-version=2023-10-19",
"CustomApiErrorPhrase": "",
"RetryAfter": null,
"ErrorSourceService": "",
"HttpHeaders": "{}"
}
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(HttpResponseMessage response, Dictionary`2 errorMapping, Activity activityForAttributes, CancellationToken cancellationToken)
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.DeviceManagement.ManagedDevices.Item.ManagedDeviceItemRequestBuilder.GetAsync(Action`1 requestConfiguration, CancellationToken cancellationToken)
at GraphAuth.GraphHelper.UpdateDeviceOwnershipAsync(String deviceId) in C:Usersa49592DesktopGraphAuthGraphHelper.cs:line 80
Interestingly, I have already used the client to make api calls and grab user information. I’ve tested these functions to verify that the client can make authorized calls and receive the correct data. I have also updatef my api permissions so that DeviceManagementManagedDevices.ReadWrite.All is set.
My expectation was to pull down the managed device and update the ManagedDeviceOwnerType to company. Here is the current function I’m using:
public async static Task UpdateDeviceOwnershipAsync(string deviceId)
{
// Ensure client isn't null
_ = _appClient ??
throw new System.NullReferenceException("Graph has not been initialized for app-only auth.");
try
{
// Error occurs here, believe it has something to do
// with API permissions, as the data isn't getting
// sent back down
ManagedDevice? device = await _appClient.DeviceManagement.ManagedDevices[deviceId].GetAsync();
}
catch (Exception ex)
{
Console.WriteLine($"ERROR:n{ex.ToString()}");
}
}
And here is how I’m calling it inside my Program.cs file:
static async Task UpdateDeviceAsync()
{
Console.WriteLine("Enter the device ID to update:");
string? deviceId = Console.ReadLine();
if (string.IsNullOrEmpty(deviceId))
{
Console.WriteLine("Device ID cannot be empty.");
return;
}
try
{
await GraphHelper.UpdateDeviceOwnershipAsync(deviceId);
Console.WriteLine("Device ownership type updated successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error updating device ownership type:n{ex.Message}");
}
}
Please let me know if there is any other info I can pass along to help debug my issue. Like I said, I would have assumed my api permissions may not be correct, but I have already checked that correct permission type has been set and I’m not sure what else it could be.