我正在尝试将google日历与我的rails应用程序同步。我一直在跟踪Google:有效地同步资源提供的文档。
我的目标是在未来一年内只同步事件,并将反复出现的事件分解为单个事件,这样我就不必处理重复规则的复杂性,也不必为重复的父事件创建子事件。
在初始同步期间,我将time_max设置为未来的一年,而在初始同步期间,我只会在未来一年内得到重复发生的事件。
当我执行增量同步时,我传递同步令牌,并期望在初始同步的time_max之后的一年内得到重复发生的事件,但这不是我所看到的。我看到的事情远远超过一年(~10年)。
在增量同步期间,我无法设置time_max,因为我从谷歌获得了预期的错误。
捕获错误syncTokenWithRequestRestrictions:同步令牌不能与其他请求限制一起使用。
下面是我用于将事件从google同步到我的应用程序的代码
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是否应该存储所需事件的时间范围?
还有其他方法可以限制增量同步的时间范围吗?
发布于 2017-01-25 18:36:54
最后,我删除了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)
endhttps://stackoverflow.com/questions/41838248
复制相似问题