-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal_api.js
95 lines (76 loc) · 2.98 KB
/
cal_api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module.exports = function (app, gcal) {
/*
===========================================================================
Google Calendar
===========================================================================
*/
app.all('/cal', function (req, res) {
req.session.lastUrl = req.originalUrl;
if (!req.session.access_token) return res.redirect('/auth');
//Create an instance from accessToken
var accessToken = req.session.access_token;
gcal(accessToken).calendarList.list(function (err, data) {
if (err) return res.send(500, err);
return res.send(data);
});
});
app.all('/cal/:calendarId', function (req, res) {
req.session.lastUrl = req.originalUrl;
if (!req.session.access_token) return res.redirect('/auth');
//Create an instance from accessToken
var accessToken = req.session.access_token;
var calendarId = req.params.calendarId;
gcal(accessToken).events.list(calendarId, {
maxResults: 10,
timeMin: new Date().toISOString()
}, function (err, data) {
if (err) return res.send(500, err);
console.log(data)
if (data.nextPageToken) {
gcal(accessToken).events.list(calendarId, {maxResults: 1, pageToken: data.nextPageToken}, function (err, data) {
console.log(data.items)
})
}
return res.send(data);
});
});
app.all('/cal/:calendarId/add', function (req, res) {
req.session.lastUrl = req.originalUrl;
if (!req.session.access_token) return res.redirect('/auth');
//Create an instance from accessToken
var accessToken = req.session.access_token;
var calendarId = req.params.calendarId;
var event = {
'summary': 'Test03',
'start': {
'dateTime': '2014-08-07T17:00:00Z'
},
'end': {
'dateTime': '2014-08-07T17:05:00Z'
},
'attendees': [
{
'email': calendarId // the user from the URL
},
{
'email': req.session.gmail_address // the logged in user
}
]
};
gcal(accessToken).events.insert(calendarId, event, function (err, result) {
return res.send(result);
});
});
app.all('/cal/:calendarId/:eventId', function (req, res) {
req.session.lastUrl = req.originalUrl;
if (!req.session.access_token) return res.redirect('/auth');
//Create an instance from accessToken
var accessToken = req.session.access_token;
var calendarId = req.params.calendarId;
var eventId = req.params.eventId;
gcal(accessToken).events.get(calendarId, eventId, function (err, data) {
if (err) return res.send(500, err);
return res.send(data);
});
});
}