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

通过Graph API一次性获取用户含认证邮箱方法的属性失败

解决Graph API无法一次性获取用户属性与认证方法的问题

问题原因

你遇到的authentication属性返回null的核心原因有两点:

  • authentication是用户资源的导航集合属性,而非直接字段,$select参数仅能筛选用户的直接属性(如displayName、otherMails),无法直接拉取导航属性的数据。
  • Graph API的/users/{userId}端点不支持通过$expand展开authentication导航属性,因此即使添加$expand=authentication也无法返回认证方法数据。

解决方案:使用批量请求(Batch Request)

通过Graph API的批量请求功能,你可以在一次HTTP调用中同时发送两个请求:一个获取用户基本属性,另一个获取该用户的认证方法,最后合并响应结果,实现“一次性获取”的需求。

C# Graph SDK 代码示例

// 创建批量请求容器
var batchRequestContent = new BatchRequestContent();

// 步骤1:添加获取用户基本属性的请求
var userBaseRequest = _graphClient.Users[userId].Request().Select("displayName,otherMails,identities");
var userRequestId = batchRequestContent.AddBatchRequestStep(userBaseRequest);

// 步骤2:添加获取用户所有认证方法的请求
var authMethodsRequest = _graphClient.Users[userId].Authentication.Methods.Request();
var authRequestId = batchRequestContent.AddBatchRequestStep(authMethodsRequest);

// 发送批量请求
var batchResponse = await _graphClient.Batch.Request().PostAsync(batchRequestContent);

// 解析并获取结果
var userBaseData = await batchResponse.GetResponseByIdAsync<User>(userRequestId);
var authMethodsData = await batchResponse.GetResponseByIdAsync<AuthenticationMethodCollectionResponse>(authRequestId);

// 最终数据:userBaseData.Body 是用户基本属性,authMethodsData.Body.Value 是所有认证方法列表

注意事项

  • 确保已配置User.Read.All(用于读取用户基本属性)和UserAuthenticationMethod.ReadWrite.All(用于读取认证方法)权限。
  • 批量请求单次最多支持20个独立请求,完全覆盖你的需求场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 01:36:30