Microsoft graph api使用返回'invalidRequest'的路径创建文件夹

时间:2017-09-30 00:32:25

标签: c# api microsoft-graph onedrive

图表api返回以下错误:

  "error": {
"code": "invalidRequest",
"message": "[child] A null value was found for the property named 'id', which has the expected type 'Edm.String[Nullable=False]'. The expected type 'Edm.String[Nullable=False]' does not allow null values.",
"innerError": {
  "request-id": "36d65bbb-2f6e-485c-b2b9-5bb7fdb76d19",
  "date": "2017-09-30T00:24:06"
  }
}

我正在使用的代码:

var url = "https://graph.microsoft.com/v1.0/me/drive/root:/joba:/children";
var folder = new ItemResource { name = "nested", folder = new FolderFacet() };
var response = api.PostAsJsonAsync<ItemResource>(url, folder).Result;
response.ThrowWhenUnsuccessful();
return response.Content.ReadAsAsync<ItemResource>().Result;

也就是说,我正在尝试在 joba 文件夹(位于 root 中)创建一个名为嵌套的嵌套文件夹。 它不起作用......我甚至试图逃脱冒号:joba:,但它也不起作用。同样的请求在graph-explorer

中工作得很好

我的错误是什么?

修改 提琴手请求

POST https://graph.microsoft.com/v1.0/me/drive/root:/wdg:/children HTTP/1.1
Accept: application/json
Authorization: Bearer <OMITED>
Content-Type: application/json; charset=utf-8
Host: graph.microsoft.com
Content-Length: 268
Expect: 100-continue
Connection: Keep-Alive

{"id":null,"createdBy":null,"createdDateTime":null,"eTag":null,"cTag":null,"description":null,"lastModifiedBy":null,"lastModifiedDateTime":null,"name":"85923635-56af-4902-82da-babd95165c6b","parentReference":null,"webUrl":null,"folder":{"ChildCount":null},"file":null}

1 个答案:

答案 0 :(得分:0)

PostAsJsonAsync使用Json.NET,默认情况下会使用null值序列化属性。由于省略值而不是故意指定空值,因此在触发时会触发OneDrive API将请求视为无效,因为为非可空字段指定了空值。

要解决此问题,您可以将以下属性添加到ItemResource类型的可空属性中:

[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]

或者,您可以使用Json.NET直接序列化对象并提供适当配置的JsonSerializerSettings实例,然后调用PostAsync