在一个驱动器中创建文件夹的图形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”:“有效负载中的属性文件夹的值与架构不匹配。”,“
有人可以帮我吗?
答案 0 :(得分:1)
有什么特殊原因不使用Microsoft Graph Client Library for .NET
?
无论如何,以下示例演示了如何通过HttpClient
create a new folder in a drive:
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();
}