首页 > 解决方案 > 有效负载中的属性文件夹的值与架构 Graph Api 不匹配

问题描述

在一个驱动器中创建文件夹的图形 API 请求是

   POST /me/drive/root/children
   Content-Type: application/json

    {
      "name": "New Folder",
      "folder": { },
      "@microsoft.graph.conflictBehavior": "rename"
    }

这但我不明白如何在 httprequest 内容正文中传递“{}”。

我的代码:

 var tt = "{ }";
var jsonData = $@"{{ ""name"": ""{txtValue}"",""folder"":""{tt}""}}";
var body = new StringContent(jsonData, Encoding.UTF8, 
"application/json"); 
apiRequest.Content = body;
apiRequest.Headers.Accept.Add(new 
MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await httpClient.SendAsync(apiRequest);
string d = response.Content.ReadAsStringAsync().Result.ToString();

但给出错误 ""code": "BadRequest","message": "payload 中的属性文件夹的值与架构不匹配。","

有人可以帮帮我吗?

标签: c#jsonmicrosoft-graph-api

解决方案


有什么特别的理由不使用Microsoft Graph Client Library for .NET这件事吗?

无论如何,以下示例演示了如何通过以下方式在驱动器中 创建新文件夹HttpClient

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    client.BaseAddress = new Uri("https://graph.microsoft.com");

    var folderPayload = new Dictionary<string, object>
    {
       ["name"] = "Test Folder",
       ["folder"] = new { },
       ["@microsoft.graph.conflictBehavior"] = "rename"
    };

   var requestContent = new StringContent(JsonConvert.SerializeObject(folderPayload));
   requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
   var response = await client.PostAsync($"/v1.0/me/drive/root/children", requestContent);
   var data = response.Content.ReadAsStringAsync().Result.ToString();
}

推荐阅读