Can I sync calendar events with the Microsoft Graph API?

谁都会走 提交于 2019-12-10 18:19:41

问题


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.

I have been successful fetching events, but I'm not getting a deltaToken back :/

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

And my sample response:

{
  "@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="
    }
  ]
}

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.

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.


回答1:


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'

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).

A few pitfalls with this:

  1. Obviously, this is not url encoded. You would need to do that.
  2. 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.
  3. 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 to events.

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.

UPDATE

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.



来源:https://stackoverflow.com/questions/39518853/can-i-sync-calendar-events-with-the-microsoft-graph-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!