1

我正在使用Microsoft Graph API來嘗試從Outlook同步日曆事件。我在尋找關於Outlook API的this article,它建議我在請求中添加標頭odata.track-changes,我會收到一個deltaToken,我可以在以後的請求中使用它來只提取那些自上次同步以來更新或創建的事件。我可以將日曆事件與Microsoft Graph API同步嗎?

我已經成功抓取事件,但我沒有得到一個deltaToken回:/

難道這僅在Outlook API支持?圖表的響應有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個月前開始同步事件,並且一直持續到未來。

如果有更高效/更好的方式來做到這一點,我樂於接受建議。如果重要,這些結果是由郵差生成的。任何幫助或澄清這一點表示讚賞。

回答

1

我結束了使用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' 

這其中,事件類型爲singleInstance(不重複發生的事件),並且lastModifiedDateTime是後終於將獲取預定2016-05-01T00:00:00Z (May 1st, 2016, midnight, UTC2099-01-01T00:00:00Z (January 1st, 2099, midnight, UTC)之間的所有日曆事件同步(在本例中爲2016-09-20T07:30:00+00:00)。

一些陷阱與此:

  1. 顯然,這不是URL編碼。你需要這樣做。
  2. 請確保 lastModifiedDateTime示例中的+正確編碼爲%2B,否則Graph API會將其視爲空間並拒絕它。
  3. 如果您不過濾循環事件,您將從現在到2099年獲得每個循環事件。這是獲取calendarViews列表的性質,而不是events

如果我可以再次這樣做,我可能會回去,只是做完整的日曆同步,哪些Graph支持(我相信)。我只是不想同步整個日曆,只是日期範圍,但似乎這是一個註定要失敗的努力。

儘管缺乏重複發生的事件,它仍然有效。

UPDATE

我結束了再殺此實現,因爲我已經保持數據同步的完整性,缺乏經常性的活動中遇到的陷阱不斷的居多,等​​相反,我拉日曆事件現實時間,並保持一個緩存。只是一些建議,以防其他人在我的情況下結束。

+0

嘿,你能幫我解釋一下你用什麼方法嗎?你是如何管理只提取更新事件的?你們每次都把所有的事情都拉了嗎? –

+0

我最終做的是,他們不是將他們的日曆事件下載到我的數據庫,而是在他們使用相同的端點(calendarView)訪問頁面時實時獲取事件。您可以使用lastModifiedDateTime在特定時間後修改事件,但我不需要,因爲我沒有同步任何內容(更容易)。用戶有一個日曆,可顯示來自兩種不同日曆服務(包括Outlook)的事件。因此,當他們點擊1月份時,我從1月份的Outlook calendarView中獲取事件,並將它們全部存儲在我的服務器上。 –

相關問題