我可以将日历事件与 Microsoft Graph API 同步吗?

Can I sync calendar events with the Microsoft Graph API?

我正在使用 Microsoft Graph api 尝试从 Outlook 同步日历事件。我正在查看有关 Outlook api 的 this article,它建议我将 header odata.track-changes 添加到我的请求中,我会收到一个 deltaToken,我可以在以后的请求中使用以仅获取自上次同步以来已更新或创建的那些事件。

我已成功获取事件,但我没有取回 deltaToken :/

只有 Outlook api 支持吗? Graph 的响应有 Preference-Applied: odata.track-changes,所以它承认我的 header。这是我的示例请求:

GET /v1.0/me/calendar/calendarView
    ?startDateTime=2016-09-01T00:00:00.0000000
    &endDateTime=2099-01-01T00:00:00.0000000
    HTTP/1.1
Host: graph.microsoft.com
Authorization: Bearer XXX
Prefer: odata.track-changes
Prefer: odata.maxpagesize=3  //for testing
Cache-Control: no-cache

我的回复示例:

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('')/calendar/calendarView",
  "value": [
    {
      "@odata.etag": "",
      "id": "",
      "createdDateTime": "2016-08-04T14:00:25.8552351Z",
      "lastModifiedDateTime": "2016-08-25T14:43:54.9950828Z",
      "changeKey": "",
      "categories": [
        "Orange category"
      ],
      "originalStartTimeZone": "Eastern Standard Time",
      "originalEndTimeZone": "Eastern Standard Time",
      "responseStatus": {
        "response": "organizer",
        "time": "0001-01-01T00:00:00Z"
      },
      "iCalUId": "",
      "reminderMinutesBeforeStart": 15,
      "isReminderOn": true,
      "hasAttachments": false,
      "subject": "Closing on House",
      "body": {
        "contentType": "html",
        "content": ""
      },
      "bodyPreview": "",
      "importance": "normal",
      "sensitivity": "normal",
      "start": {
        "dateTime": "2016-09-08T19:30:00.0000000",
        "timeZone": "UTC"
      },
      "end": {
        "dateTime": "2016-09-08T21:30:00.0000000",
        "timeZone": "UTC"
      },
      "location": {
        "displayName": "245 E Main St",
        "address": {
          "street": "245 E Main St",
          "city": "Somewhere",
          "state": "NY",
          "countryOrRegion": "United States",
          "postalCode": ""
        }
      },
      "isAllDay": false,
      "isCancelled": false,
      "isOrganizer": true,
      "recurrence": null,
      "responseRequested": true,
      "seriesMasterId": null,
      "showAs": "busy",
      "type": "singleInstance",
      "attendees": [],
      "organizer": {
        "emailAddress": {
          "name": "",
          "address": ""
        }
      },
      "webLink": "https://outlook.office365.com/owa/?ItemID="
    },
    {
      "@odata.etag": "",
      "id": "",
      "createdDateTime": "2016-08-19T18:02:39.0607411Z",
      "lastModifiedDateTime": "2016-08-19T18:04:10.548447Z",
      "changeKey": "",
      "categories": [
        "Green category"
      ],
      "originalStartTimeZone": "UTC",
      "originalEndTimeZone": "UTC",
      "responseStatus": {
        "response": "organizer",
        "time": "0001-01-01T00:00:00Z"
      },
      "iCalUId": "",
      "reminderMinutesBeforeStart": 15,
      "isReminderOn": true,
      "hasAttachments": false,
      "subject": "Moving (off work)",
      "body": {
        "contentType": "html",
        "content": ""
      },
      "bodyPreview": "",
      "importance": "normal",
      "sensitivity": "normal",
      "start": {
        "dateTime": "2016-09-10T00:00:00.0000000",
        "timeZone": "UTC"
      },
      "end": {
        "dateTime": "2016-09-13T00:00:00.0000000",
        "timeZone": "UTC"
      },
      "location": {
        "displayName": "",
        "address": {}
      },
      "isAllDay": true,
      "isCancelled": false,
      "isOrganizer": true,
      "recurrence": null,
      "responseRequested": true,
      "seriesMasterId": null,
      "showAs": "oof",
      "type": "singleInstance",
      "attendees": [],
      "organizer": {
        "emailAddress": {
          "name": "",
          "address": ""
        }
      },
      "webLink": "https://outlook.office365.com/owa/?ItemID="
    },
    {
      "@odata.etag": "",
      "id": "",
      "createdDateTime": "2016-09-13T19:05:20.8438647Z",
      "lastModifiedDateTime": "2016-09-13T19:05:22.1899702Z",
      "changeKey": "",
      "categories": [],
      "originalStartTimeZone": "America/New_York",
      "originalEndTimeZone": "America/New_York",
      "responseStatus": {
        "response": "organizer",
        "time": "0001-01-01T00:00:00Z"
      },
      "iCalUId": "",
      "reminderMinutesBeforeStart": 15,
      "isReminderOn": true,
      "hasAttachments": false,
      "subject": "Coffee Break",
      "body": {
        "contentType": "html",
        "content": ""
      },
      "bodyPreview": "",
      "importance": "normal",
      "sensitivity": "normal",
      "start": {
        "dateTime": "2016-09-15T20:15:00.0000000",
        "timeZone": "UTC"
      },
      "end": {
        "dateTime": "2016-09-15T21:15:00.0000000",
        "timeZone": "UTC"
      },
      "location": {
        "displayName": "",
        "address": {}
      },
      "isAllDay": false,
      "isCancelled": false,
      "isOrganizer": true,
      "recurrence": null,
      "responseRequested": true,
      "seriesMasterId": null,
      "showAs": "busy",
      "type": "singleInstance",
      "attendees": [],
      "organizer": {
        "emailAddress": {
          "name": "",
          "address": ""
        }
      },
      "webLink": "https://outlook.office365.com/owa/?ItemID="
    }
  ]
}

我删除了我认为可能略微敏感的所有内容。最终,我的 Laravel 应用试图同步 4 个月前开始的事件,并一直持续到未来。

如果有更多 efficient/better 的方法,我愿意接受建议。如果重要的话,这些结果是使用 Postman 生成的。感谢您对此提供任何帮助或澄清。

我最终像这样使用 odata filter

https://graph.microsoft.com/beta/me/calendar/calendarView?startDateTime=2016-05-01T00:00:00Z&endDateTime=2099-01-01T00:00:00Z&$filter=type eq 'singleInstance' and lastModifiedDateTime eq '2016-09-20T07:30:00+00:00'

这将获取安排在 2016-05-01T00:00:00Z (May 1st, 2016, midnight, UTC2099-01-01T00:00:00Z (January 1st, 2099, midnight, UTC) 之间的所有日历事件,其中事件类型是 singleInstance(不是重复事件)并且 lastModifiedDateTime 在上次同步(在此示例中,2016-09-20T07:30:00+00:00)。

一些陷阱:

  1. 显然,这不是 url 编码。你需要这样做。
  2. 确保 lastModifiedDateTime 示例中的 + 正确编码为 %2B,否则图表 API 会将其视为 space 并拒绝它。
  3. 如果您不过滤掉重复发生的事件,您将获得从现在到 2099 年的每个重复发生的事件。这是获取 calendarViews 列表而不是 events 的本质。

如果我能再做一次,我可能会回去只做完整的日历同步,Graph 支持(我相信)。我只是不想同步整个日历,只同步一个日期范围,但似乎这注定要失败。

但是尽管没有重复发生的事件,它还是有效的。

更新

我最终放弃了这个实现,主要是因为我在维护数据同步完整性、缺少重复事件等方面遇到了持续的陷阱。相反,我实时提取日历事件,并维护一个缓存。只是一些建议,以防其他人遇到我的情况。