使用Microsoft Graph API分配Planner任务报错:qualifiedName参数不能为空
问题原因与解决方案
这个错误的根源在于你构建assignments字段的格式不符合Microsoft Graph API的隐性要求——虽然官方文档可能没特别强调,但每个分配条目必须明确声明OData类型,否则API内部无法正确识别对象结构,进而抛出qualifiedName为空的内部错误。
具体问题分析
你的代码中,为每个用户创建的分配对象是一个空对象new {}:
assignees.Add(assignment, new { });
Graph API无法识别这个空对象的类型,导致内部解析时触发了qualifiedName参数为空的异常。
修正步骤
为分配对象添加OData类型声明
每个分配条目需要包含@odata.type字段,值为#microsoft.graph.plannerAssignment,让API明确知道这是一个Planner分配对象:Dictionary<string, object> assignees = new Dictionary<string, object>(); foreach(string userId in assignments.Keys) { assignees.Add(userId, new { @odata.type = "#microsoft.graph.plannerAssignment" }); }改用RestSharp的原生JSON请求体方法
你当前手动序列化JSON再通过AddParameter传递的方式,容易出现格式或编码问题。建议直接使用RestSharp的AddJsonBody方法,它会自动处理序列化细节:// 移除手动序列化的代码,替换为: TaskRequest.AddJsonBody(body);验证用户ID的有效性
确保assignments字典的Key是有效的Azure AD用户Object ID,格式错误的ID也可能引发类似的解析异常。
修正后的完整方法示例:
public TaskResponse CreateTask(string planId, string bucketId, string title, Dictionary<string, Assignment> assignments = null, DateTime? dueDateTime = null) { RestRequest TaskRequest = new RestRequest("planner/tasks", Method.POST) { RequestFormat = DataFormat.Json }; Dictionary<string, object> assignees = new Dictionary<string, object>(); if (assignments != null) { foreach(string userId in assignments.Keys) { assignees.Add(userId, new { @odata.type = "#microsoft.graph.plannerAssignment" }); } } var body = new { planId, bucketId, title, assignments = assignees, dueDateTime }; TaskRequest.AddJsonBody(body); // 改用AddJsonBody IRestResponse TaskResponse = GraphClient.Execute(TaskRequest); if (!TaskResponse.IsSuccessful) { return null; } var result = JsonConvert.DeserializeObject<TaskResponse>(TaskResponse.Content); return result; }
内容的提问来源于stack exchange,提问作者Peter Gordon
相关产品推荐
相关产品推荐

