c# - Microsoft.Graph.Models.ODataErrors.ODataError 与 .PatchAsync(用户)上的 Graph API

标签 c# .net azure microsoft-graph-api

不太确定如何解决此错误,因为当我捕获错误消息时它不具体

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;
}

回应:

enter image description here

为了解决该错误,我通过授予管理员同意在应用程序中添加了 User.ReadWrite.All 类型的 Application 权限:

enter image description here

当我现在再次运行相同的代码时,我成功地得到了响应,如下所示:

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;
}

回应:

enter image description here

为了确认这一点,我在门户中检查了相同的内容,其中用户的扩展程序属性更新了,如下所示:

enter image description here

关于c# - Microsoft.Graph.Models.ODataErrors.ODataError 与 .PatchAsync(用户)上的 Graph API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77119726/

相关文章:

c# - Entity Framework 在调用 INSERT 之前加载所有行

c# - 为什么 Calli 比委托(delegate)调用更快?

.net - Process.Start(url) 在 Windows 8/Chrome 上损坏 - 有替代方案吗?

wcf - 在 Windows Azure 中部署 WCF : One or more role instances are unhealthy. 回收..未处理的异常 : System. NullReferenceException?

java - 使用 Math.Floor 比显式整数转换有什么好处吗?

c# - 将除法舍入为整数的正确方法是什么?

c# - 为什么这个 .NET Core 包与我的 PCL 不兼容?

c# - csv 文件导入 Azure 存储时出现数据格式问题

Azure-警报 : Unable to create azure alert in gov subscription

c# - Microsoft HoloLens 图像捕获不起作用