Reputation: 101
I have been implementing some functions of google calendar api. I have a custom calendar which have the capability to sync with google calendar. This means, you can create, and edit calendars and events from my dashboard to google calendar account. The main problem I am facing is, if I update the event directly from my google calendar. I have implemented push notifications and getting response like this:
{
"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"
}
But this response is very general. For example, If I have 1000 events on this calendar, and update the complete 1000 events. I will receive always the same notification 1000. I would like to know, if it is possible to get which event id has change, so I can perform and update to my DB.
The way I init the watch is like this:
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});
}
});
});
}
Upvotes: 1
Views: 1485
Reputation: 101
For people who are looking for a good way to get which events changed when the web hook triggers. When your web hook is triggered, you will get something like this:
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
On X-Goog-Resource-URI you will get something like this:
https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?alt=json
With you OAuth authentication, you can make a GET request to this URL to fetch all events that belongs to this calendar. Now the trick to know which resources have been changed is really simple. For each event you will get something like this:
{
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: ''
}
As you can see, the is a sequence: 0, this is incremental. This means, each time you apply some changes on your event (summary, description, start date, end date, etc). This number will be incremented +1. You can save this on your database, so each time web hook triggers, you update only events which sequence is > than saved sequence. So basically, you update the event and save the new value of sequence. Next time that web hook triggers, it will only update on your DB the events that are included in this condition.
Hope it helps, happy coding.
Upvotes: 8