list-example.js
2.93 KB
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
96
97
98
99
100
101
102
103
104
var util = require('util');
var express = require('express');
var config = require('./config');
var gcal = require('../GoogleCalendar');
/*
===========================================================================
Setup express + passportjs server for authentication
===========================================================================
*/
var app = express();
var passport = require('passport')
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
app.configure(function() {
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
});
app.listen(8082);
passport.use(new GoogleStrategy({
clientID: config.consumer_key,
clientSecret: config.consumer_secret,
callbackURL: "http://localhost:8082/auth/callback",
scope: ['openid', 'email', 'https://www.googleapis.com/auth/calendar']
},
function(accessToken, refreshToken, profile, done) {
profile.accessToken = accessToken;
return done(null, profile);
}
));
app.get('/auth',
passport.authenticate('google', { session: false }));
app.get('/auth/callback',
passport.authenticate('google', { session: false, failureRedirect: '/login' }),
function(req, res) {
req.session.access_token = req.user.accessToken;
res.redirect('/');
});
/*
===========================================================================
Google Calendar
===========================================================================
*/
app.all('/', function(req, res){
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('/:calendarId', function(req, res){
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:1}, 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('/:calendarId/:eventId', function(req, res){
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);
});
});