尝试将文件附加到 Outlook 日历事件时出现状态代码 422

Dmi*_*try 5 javascript microsoft-graph-calendar microsoft-graph-api

我正在尝试使用graph-js-sdk-web.js我相信我完全按照说明将文本文件附加到 Outlook 中的现有事件,但我得到了422响应。我如何让它附加文件?

我很确定我eventID是正确的,因为我可以创建一个事件并通过该 ID 获取它。我很确定我的文件的后加载是正确的,因为我自己附加了文件,然后通过扩展附件的 id 获取了该事件,并且它是相同的 OData 类型、名称和内容。

到目前为止,我在 google 上搜索了响应,我所看到的一切要么只对人们有效,要么与该示例相比,他们对代码不感兴趣。

这是我请求的权限

openid profile User.Read MailboxSettings.Read Calendars.ReadWrite
Run Code Online (Sandbox Code Playgroud)

这匹配授予注册应用程序的权限。

这是客户端和文件附件代码:

// Create a Graph client
var client = MicrosoftGraph.Client.init({
    authProvider: (done) => {
        // Just return the token
        done(null, sessionStorage.accessToken);
    }
});

client
    .api('/me/calendar/events/' + eventID + '/attachments')
    .post({
        attachment: {
            "@odata.type": "#microsoft.graph.fileAttachment",
            name: "helloworld.txt",
            contentBytes: "SGVsbG8gd29ybGQh"
        }
    }, (error, response) => {
        if (error)
            console.log(error);
        else {
            console.log(response);
        }
    });
Run Code Online (Sandbox Code Playgroud)

产生这个请求有效载荷的

{
  "attachment":{
    "@odata.type":"#microsoft.graph.fileAttachment",
    "name":"helloworld.txt",
    "contentBytes":"SGVsbG8gd29ybGQh"
  }
}
Run Code Online (Sandbox Code Playgroud)

我从中得到的回应是

{
  "error": {
    "code": "UnprocessableType",
    "message": "Cannot process input of abstract type 'Microsoft.OutlookServices.Attachment'",
    "innerError": {
      "request-id": "0a81e9f9-ef64-4b5e-b854-65e24fb71cfb",
      "date": "2019-05-14T23:57:29"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我没有看到处理附件需要什么。我觉得奇怪的是它的抽象基类,而不是我在odata.type现场提供的那个,它可能什么都不是。

我打开了图形资源管理器,即使他们没有用于附加到事件的工作示例,我也使用了带有此有效负载和我的 url 的帖子,并得到了完全相同的响应 422。这告诉我它不是 js 库,它与graph api 本身,要么设置与其文档不同,要么我们缺少一些未记录的设置要求。

Dmi*_*try 1

感谢 jasonjon 的帮助,问题解决了。在引用的说明中,有效负载示例和 JavaScript 代码示例不匹配。我使用了 js 示例,并在有效负载中添加了父节点附件。结果发现没有父节点。使用该api的正确方法是

client
    .api('/me/calendar/events/' + eventID + '/attachments')
    .post({
       "@odata.type": "#microsoft.graph.fileAttachment",
       name: "helloworld.txt",
       contentBytes: "SGVsbG8gd29ybGQh"
    }, (error, response) => {
        if (error)
            console.log(error);
        else {
            console.log(response);
        }
    });
Run Code Online (Sandbox Code Playgroud)