【问题标题】:Microsoft graph api create folder using path returning 'invalidRequest'Microsoft graph api使用返回“invalidRequest”的路径创建文件夹
【发布时间】:2017-09-30 00:32:25
【问题描述】:

graph 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 内)中创建一个名为 nested 的嵌套文件夹。 它不起作用......我什至试图逃避冒号: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}

【问题讨论】:

  • 一目了然,您的请求看起来符合预期,我不会使用与您的几乎相同的代码来重现它。如果您可以捕获通过线路发送到 Graph 的请求和响应,并将其添加到您的问题中,这将有所帮助(排除身份验证令牌:))
  • 查看提琴手请求,我已经弄明白了。我正在使用相同的模型来检索数据和发布数据,并且我在那里发布 null 东西并且图表不喜欢它

标签: c# api microsoft-graph-api onedrive


【解决方案1】:

PostAsJsonAsync 在后台使用 Json.NET,默认情况下会序列化具有空值的属性。由于省略值与故意指定空值的处理方式不同,因此当发生这种情况时,它会触发 OneDrive API 认为请求无效,因为为不可为空的字段指定了空值。

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

[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    相关资源
    最近更新 更多