不太确定如何解决此错误,因为当我捕获错误消息时它不具体
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)
它在这一行特别出错:
await graphClient.Users[$"{id}"].PatchAsync(user);
有关更多上下文,我正在尝试更新用户 onPremisesExtensionAttributes
在其他地方的另一种方法中,我像这样创建它们
var user = new User
{
OnPremisesExtensionAttributes = new OnPremisesExtensionAttributes
{
ExtensionAttribute1 = "123"
ExtensionAttribute2 = "abc"
}
};
根据 Microsoft 文档,一切都是正确的。我不太确定如何继续这个问题,我花了 2 个小时阅读它,但没有任何解决办法。它一直在工作,直到我被要求重新访问这个项目并更新所有依赖项。不过,根据文档,这就是您应该使用 Graph SDK v5 修补用户的方式
非常感谢任何帮助
最佳答案
当我通过包含 try-catch 方法运行下面的代码时,我在控制台中遇到了相同的错误,并且权限不足
如下:
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;
}
回应:
为了解决该错误,我通过授予管理员同意在应用程序中添加了 User.ReadWrite.All
类型的 Application 权限:
当我现在再次运行相同的代码时,我成功地得到了响应,如下所示:
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;
}
回应:
为了确认这一点,我在门户中检查了相同的内容,其中用户的扩展程序属性更新了,如下所示:
关于c# - Microsoft.Graph.Models.ODataErrors.ODataError 与 .PatchAsync(用户)上的 Graph API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77119726/