我正在使用 Microsoft Graph API 尝试从 Outlook 同步日历事件。我在看this article关于 Outlook api,它建议我将 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 个月前开始的事件,并永远持续到 future 。
如果有更有效/更好的方法来做到这一点,我愿意接受建议。如果重要的话,这些结果是由 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, UTC
之间安排的所有日历事件和2099-01-01T00:00:00Z (January 1st, 2099, midnight, UTC)
其中事件类型为 singleInstance
(不是重复发生的事件)和 lastModifiedDateTime
是在上次同步之后(在本例中为 2016-09-20T07:30:00+00:00
)。
一些陷阱:
- 显然,这不是 url 编码的。你需要这样做。
- 确保lastModifiedDateTime 示例中的 + 已正确编码为
%2B
,否则 Graph API 会将其视为空格并拒绝它。 - 如果您不过滤掉重复事件,您将获得从现在到 2099 年的每个重复事件。这就是获取
calendarViews
列表的本质。与events
相反.
如果我能再次这样做,我可能会回去并只进行完整的日历同步,Graph 支持(我相信)。我只是不想同步整个日历,只想同步一个日期范围,但这似乎是注定失败的尝试。
尽管没有重复发生的事件,但它仍然有效。
更新
我最终放弃了这个实现,主要是因为我在维护数据同步完整性、缺乏重复事件等方面遇到了持续的陷阱。相反,我实时提取日历事件,并维护缓存。只是一些建议,以防其他人遇到我的情况。
关于outlook - 我可以将日历事件与 Microsoft Graph API 同步吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39518853/