2017-01-24 40 views
0

我想與我的rails應用程序同步谷歌日曆。我一直在關注Google提供的文檔: Synchronize Resources Efficiently谷歌日曆增量同步與初始time_max

我的目標是隻將事件同步到未來一年,並將重複事件分解爲單個事件,這樣我就不必處理重複規則的複雜性併爲重複性父事件創建子事件。

在初始同步過程中,我將time_max設置爲將來1年,並且在初始同步期間,我只能獲得長達一年的重複事件。

當我執行增量同步時,我傳遞同步令牌並期望將週期性事件限制在初始同步的time_max的一年內,但這不是我所看到的。我看到一年以上(〜10年)的事件。

在增量同步過程中,我無法設置time_max,因爲我收到了Google提供的預計的錯誤。

捕獲錯誤syncTokenWithRequestRestrictions:同步令牌不能與其他請求限制一起使用。

下面是我用從谷歌事件同步到我的應用程序代碼

def sync_from_google 
    next_page_token = nil 

    begin 
     if sync_token.nil? # full sync 
     response = @calendar_service.list_events(google_id, 
      time_min: Time.now.utc.strftime("%FT%TZ"), 
      time_max: 1.year.from_now.utc.strftime("%FT%TZ"), 
      single_events: true) 
     else # incremental sync 
     response = @calendar_service.list_events(google_id, 
      sync_token: sync_token, 
      page_token: next_page_token, 
      single_events: true) 
     end 

     response.items.each do |gevent| 
     GoogleCalendarEvent.create_event(self.id, gevent, nil) 
     end 
     next_page_token = response.next_page_token 

    rescue Google::Apis::ClientError => error 
     if error.status_code == 410 
     self.unsync 
     end 
    end while (response.next_sync_token.nil?) 

    update_attributes(synced: true, sync_token: response.next_sync_token) 
end 

我是不是一個虛擬的和缺少明顯的東西?

初始同步提供的sync_tokens是否應存儲所需事件的時間範圍?

有沒有其他一些方法可以限制增量同步的時間範圍?

+0

的情況下,增加的方法來循環你嘗試刪除'time_max',看看這仍然會發生?如果不是,則可能是其中一個篩選器同步標記不兼容,如[示例代碼](https://developers.google.com/google-apps/calendar/v3/sync#sample_code)中所述。 –

+0

增量同步時允許SingleEvents = true(只是timeMin和timeMax不是,您應該過濾增量同步附帶的不感興趣的條目) – luc

回答

1

我最終刪除了single_events參數,然後手動循環定期time_min & time_max的週期性事件的實例。

這是更新後的代碼,任何人在未來都會遇到這種情況。

def sync_from_google 
    next_page_token = nil 

    begin 
     if sync_token.nil? # full sync 
     response = @calendar_service.list_events(google_id, 
      time_min: Time.now.utc.strftime("%FT%TZ"), 
      time_max: 1.year.from_now.utc.strftime("%FT%TZ")) 
     else # incremental sync 
     response = @calendar_service.list_events(google_id, 
      sync_token: sync_token, 
      page_token: next_page_token) 
     end 

     response.items.each do |gevent| 
     if gevent.recurrence 
      sync_reccuring_events(gevent) 
     else 
      GoogleCalendarEvent.create_event(self.id, gevent, nil) 
     end 
     end 
     next_page_token = response.next_page_token 

    rescue Google::Apis::ClientError => error 
     if error.status_code == 410 
     self.unsync 
     end 
    end while (response.next_sync_token.nil?) 

    update_attributes(synced: true, sync_token: response.next_sync_token) 
end 

,並通過經常性事件

def sync_reccuring_events(google_event) 
    next_page_token = nil 

    begin 
     response = calendar_service.list_event_instances(google_id, 
     google_event.id, 
     time_min: Time.now.utc.strftime("%FT%TZ"), 
     time_max: 1.year.from_now.utc.strftime("%FT%TZ"), 
     page_token: next_page_token) 

     response.items.each do |gevent| 
      GoogleCalendarEvent.create_event(self.id, gevent, nil) 
     end 
     next_page_token = response.next_page_token 

    end while (next_page_token) 
end