2016-04-12 71 views
1

我一直在實現谷歌日曆API的一些功能。我有一個自定義日曆,可以與Google日曆同步。這意味着,您可以創建和編輯日曆和事件,從我的儀表板到Google日曆帳戶。我面臨的主要問題是,如果我直接從我的谷歌日曆更新事件。我實施了推送通知並獲得如下響應:當谷歌日曆事件發生變化時更新數據庫

{ 
     "Google_channel_ID": "19835150 - 0057 - 11e6 - bc9c - 1735798 ca540", 
     "Google_channel_token": "calendar_id = 4e7 d5c20ht9lpfdtuukcsvgeq4 @group.calendar.google.com & user_id = 43e d7rxeqqce3fk09ahszc", 
     "Google_channel_expiration": "Tue,12 Apr 2016 12: 34: 36 GMT", 
     "Google_resource_id": "oLMEEhHAw5Xo3r2187CWykjAtm0", 
     "Google_resource_URI": "https: //www.googleapis.com/calendar/v3/calendars/[email protected]/events?alt=json", 
     "Google_resource_state": "sync", 
     "Google_message_number": "1" 
    } 

但是,這種迴應非常普遍。例如,如果我在此日曆上有1000個事件,並更新完整的1000個事件。我會收到總是相同的通知1000.我想知道,如果有可能得到哪個事件ID有變化,所以我可以執行並更新到我的數據庫。

我初始化表的方法是這樣的:

exports.watch = function(req, res){ 
     var channel_id  = uuid.v1(); 
     var user_id  = req.body.user_id; 
     var calendar_id  = req.body.calendar_id; 

     authorize(user_id, function(oauth2Client){ 
      var data = { 
      auth: oauth2Client, 
      calendarId: calendar_id, 
      resource: { 
       id: channel_id, 
       token: 'calendar_id='+ calendar_id + '&user_id=' + user_id, 
       address: 'https://api.medradr.com/google-watch', 
       type: 'web_hook', 
       params: { 
        ttl: '36000' 
      } 
     } 
     }; 
     calendar.events.watch(data, function (err, response) { 
      if (err) { 
       console.error('The API returned an error: ' + err); 
       return; 
      }else{ 
       res.send({ok: true, message: 'Listener created', result:response}); 
      } 
     }); 
    }); 
    } 
+0

重複。問題已在此[線程](http://stackoverflow.com/questions/18206024/update-database-if-change-in-google-calendar-event)中得到解答。 – noogui

+0

該線程沒有正確回答。如果您閱讀我的第一個問題,您會看到我的推送通知已成功配置,並且每次在該日曆上發生某些事情時,我都會收到推送通知。真正的問題是,如何現在哪些事件改變了推送通知觸發器?我已經找到了解決方案,並在這裏發佈答案。 –

回答

4

對於誰是尋找一個良好的方式來獲得該事件的人改變了網絡掛接時觸發。如果您的網絡鉤被觸發時,你會得到這樣的事情:

X-Goog-Channel-ID: channel-ID-value 
    X-Goog-Channel-Token: channel-token-value 
    X-Goog-Channel-Expiration: expiration-date-and-time // In human-readable format; present only if channel expires. 
    X-Goog-Resource-ID: identifier-for-the-watched-resource 
    X-Goog-Resource-URI: version-specific-URI-of-the-watched-resource 
    X-Goog-Resource-State: sync 
    X-Goog-Message-Number: 1 

X-goog-資源-URI你會得到這樣的事情:

https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?alt=json 

隨着你OAuth認證,您可以向此URL發出GET請求以獲取屬於此日曆的所有事件。現在知道哪些資源已被更改的技巧非常簡單。對於每一個事件,你會得到這樣的:

 { 
      event_id: 335, 
      user_id: '43ed7rxeqqce3fk09ahszc', 
      kind: 'calendar#event', 
      etag: '"2921213870180000"', 
      google_id: 'cpbcesg966skprb3rh1p1ud668', 
      status: 'confirmed', 
      htmlLink: 'https://www.google.com/calendar/event?eid=Y3BiY2VzZzk2NnNrcHJiM3JoMXAxdWQ2NjggNGU3ZDVjMjBodDlscGZkdHV1a2NzdmdlcTRAZw', 
      created: Thu Apr 14 2016 00:00:00 GMT-0500 (CDT), 
      updated: Thu Apr 14 2016 00:00:00 GMT-0500 (CDT), 
      summary: 'Testing google notifications', 
      description: '', 
      creator: '[email protected]', 
      organizer: '[email protected]', 
      start: Sat Apr 09 2016 00:00:00 GMT-0500 (CDT), 
      end: Sun Apr 10 2016 00:00:00 GMT-0500 (CDT), 
      iCalUID: '[email protected]', 
      event_type_id: 0, 
      calendar_id: 0, 
      timezone_id: 0, 
      sequence: 0, 
      calendar_color_id: '' 
    } 

正如你所看到的,是一個序列:0,這是增量。這意味着,每次您對事件進行一些更改(摘要,說明,開始日期,結束日期等)。該數字將遞增+1。您可以將其保存到數據庫中,因此每次Web鉤子觸發時,只更新序列>保存序列的事件。所以基本上,你更新事件並保存序列的新值。下一次該web鉤子觸發時,它只會在您的數據庫上更新包含在此條件中的事件。

希望它有幫助,快樂編碼。

相關問題