You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MsGraph API更新Azure AppReg时遇Request_ResourceNotFound错误

问题解决:Graph API调用AppReg时Request_ResourceNotFound错误

核心原因

你用appId(客户端ID)作为graphClient.Applications["{id}"]的定位参数,但Graph API中Applications集合的主键是对象ID(id字段),而非appId。这就是为什么你用Filter基于appId能查到资源,但直接用appId调用单资源方法会提示找不到。

解决步骤

1. 从查询结果中提取对象ID

在你现有的查询代码中,获取返回的Application对象的id属性,这才是后续调用需要的主键:

var queryResult = await graphClient.Applications.GetAsync((requestConfiguration) =>
{
    requestConfiguration.QueryParameters.Filter = "appId eq 'Myapplication-id'";
    requestConfiguration.QueryParameters.Count = true;
    requestConfiguration.QueryParameters.Top = 1;
    requestConfiguration.QueryParameters.Orderby = new string[] { "displayName" };
    requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
});

// 提取对象ID
var applicationObjectId = queryResult.Value.FirstOrDefault()?.Id;
if (string.IsNullOrEmpty(applicationObjectId))
{
    // 处理未找到的情况
    return;
}

2. 使用对象ID调用AddPassword方法

替换原来的Myapplication-id为上面获取的applicationObjectId:

var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Applications.Item.AddPassword.AddPasswordPostRequestBody
{
    PasswordCredential = new PasswordCredential
    {
        DisplayName = "Password friendly name",
    },
};
var result = await graphClient.Applications[applicationObjectId].AddPassword.PostAsync(requestBody);

3. Patch方法的正确用法(可选)

如果用Patch方法添加密码,注意不要直接替换PasswordCredentials列表(会覆盖现有所有密码),应该用增量添加的方式。另外同样要使用对象ID:

var requestBody = new Application
{
    PasswordCredentials = new List<PasswordCredential>
    {
        new()
        {
            DisplayName = "Password friendly name",
            StartDateTime = DateTimeOffset.Now,
            EndDateTime = DateTimeOffset.Now.AddYears(2),
            KeyId = Guid.NewGuid()
        }
    }
};

var result = await graphClient.Applications[applicationObjectId].PatchAsync(requestBody);

注意:Patch操作中,若只新增密码,上述写法是可行的;但如果需要保留原有密码,建议使用AddPassword专用方法,它会自动在现有列表中追加新密码,避免误覆盖。

额外验证

  • 确认权限:调用AddPassword或Patch需要Application.ReadWrite.All或Directory.ReadWrite.All权限(Delegated或Application权限均可,需已授予并同意)。
  • 核对对象ID:可以在Azure门户的应用注册页,查看应用"概述"中的"对象ID"字段,和代码中提取的id对比确认。

内容的提问来源于stack exchange,提问作者Mlo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:13:12