我可以使用Microsoft Graph API同步日历事件吗? [英] Can I sync calendar events with the Microsoft Graph API?
问题描述
我正在使用Microsoft Graph api尝试从Outlook同步日历事件.我正在查看本文关于Outlook api,建议我将标头odata.track-changes
添加到我的请求中,然后我将收到一个deltaToken
,我可以在以后的请求中使用它,以仅提取自上次同步以来已更新或创建的那些事件.
I'm working with the Microsoft Graph api to try and sync calendar events from Outlook. I was looking at this article concerning the Outlook api, which suggested I add the header odata.track-changes
to my request and I would receive a deltaToken
, which I could use on a later request to fetch only those events which had been updated or created since the last sync.
我已经成功获取了事件,但是我没有得到deltaToken回来:/
I have been successful fetching events, but I'm not getting a deltaToken back :/
仅在Outlook api中支持此功能吗? Graph的响应具有Preference-Applied: odata.track-changes
,因此它确认了我的标题.这是我的示例请求:
Is this only supported in the Outlook api? Graph's response has Preference-Applied: odata.track-changes
, so it's acknowledging my header. Here's my sample request:
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个月开始同步事件,并永远持续到将来.
I redacted anything I thought could be mildly sensitive. Ultimately, my Laravel app is trying to sync events starting 4 months back, and go forever into the future.
如果有更有效/更好的方法,我欢迎您提出建议.如果重要的话,这些结果是由Postman生成的.任何帮助或澄清对此表示赞赏.
If there's a more efficient/better way to do it, I'm open to suggestions. If it matters, these results were generated with Postman. Any help or clarity on this is appreciated.
推荐答案
I ended up using the odata filter like so:
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
).
This will fetch all calendar events scheduled between 2016-05-01T00:00:00Z (May 1st, 2016, midnight, UTC
and 2099-01-01T00:00:00Z (January 1st, 2099, midnight, UTC)
where the event type is singleInstance
(not a recurring event) and the lastModifiedDateTime
is after the last sync (in this example, 2016-09-20T07:30:00+00:00
).
与此有关的一些陷阱:
- 显然,这不是url编码的.您需要这样做.
- 请确保将lastModifiedDateTime示例中的+正确编码为
%2B
,否则Graph API会将其视为空格并拒绝. - 如果不过滤重复发生的事件,从现在起到2099年,您将获得每个重复发生的事件.这是获取
calendarViews
而不是events
的列表的本质.
- Obviously, this is not url encoded. You would need to do that.
- Make sure the + in the lastModifiedDateTime example is properly encoded to
%2B
, otherwise the Graph API will treat it as a space and reject it. - If you don't filter out recurring events, you will get each recurring event from now until 2099. This is the nature of fetching a list of
calendarViews
as opposed toevents
.
如果我可以再次执行此操作,我可能会回过头来,然后执行Graph支持的完整日历同步(我相信).我只是不想同步整个日历,只希望同步一个日期范围,但这似乎注定要失败.
If I could do this again, I would probably go back and just do the full calendar sync, which Graph supports (I believe). I just didn't want to sync the whole calendar, only a date range, but it seems that was a doomed endeavor.
但是,尽管没有重复发生的事件,它仍然有效.
But despite the lack of recurring events, it works.
更新
我之所以放弃此实现,主要是因为我在保持数据同步完整性,缺少重复发生的事件等方面遇到了持续的陷阱.相反,我实时提取了日历事件,并维护一个缓存.只是一些建议,以防其他人陷入我的境地.
I ended up scrapping this implementation mostly because of the continuing pitfalls I've encountered with maintaining data sync integrity, lack of recurring events, and etc. Instead, I pull calendar events in real time, and maintain a cache. Just some advice in case anyone else ends up in my situation.
这篇关于我可以使用Microsoft Graph API同步日历事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!