3

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

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

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

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 个月前开始的事件,并一直持续到未来。

如果有更有效/更好的方法来做到这一点,我愿意接受建议。如果重要的话,这些结果是由 Postman 生成的。对此的任何帮助或澄清表示赞赏。

4

1 回答 1

2

我最终使用了odata 过滤器,如下所示:

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, UTC和位置之间的所有日历事件。2099-01-01T00:00:00Z (January 1st, 2099, midnight, UTC)singleInstancelastModifiedDateTime2016-09-20T07:30:00+00:00

这样做有几个陷阱:

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

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

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

更新

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

于 2016-09-26T12:39:57.830 回答