javascript - 尝试将文件附加到 Outlook 日历事件时的状态代码 422

标签 javascript microsoft-graph-api microsoft-graph-calendar

我正在尝试使用 graph-js-sdk-web.js 将文本文件附加到 Outlook 中的现有事件我相信我遵循了 instructions exactly但我收到了 422 响应。我如何让它附加文件?

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

到目前为止,我用谷歌搜索了响应,我所看到的一切要么只适用于人们,要么与该示例相比他们的代码偏离了。

这是我请求的权限

openid profile User.Read MailboxSettings.Read Calendars.ReadWrite

这与授予已注册应用的权限相匹配。

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

// 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);
        }
    });

产生此请求负载的

{
  "attachment":{
    "@odata.type":"#microsoft.graph.fileAttachment",
    "name":"helloworld.txt",
    "contentBytes":"SGVsbG8gd29ybGQh"
  }
}

我得到的响应是

{
  "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"
    }
  }
}

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

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

最佳答案

感谢 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);
        }
    });

关于javascript - 尝试将文件附加到 Outlook 日历事件时的状态代码 422,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56140240/

相关文章:

azure-active-directory - 无法使用委派权限通过 Microsoft Graph API 发送电子邮件

microsoft-graph-api - 图 "ErrorApiQuarantined"

node.js - 使用 Microsoft Graph API 创建 Outlook 日历事件时未添加与会者

javascript - 单个 JavaScript 文件的 TypeScript 定义

javascript - 播放器未出现在 Canvas 上(Javascript + html + css)

JavaScript Jquery 作业问题只适用于变量的最佳值

c# - 如何使用 Microsoft Graph 创建 Microsoft Teams session 作为应用程序?

azure - Microsoft Graph 'created' 订阅无法使用应用程序 token

microsoft-graph-api - 如何使用 Microsoft Graph Api 访问组日历?

javascript - 在for循环中以 Angular 2链接http调用