server.js
4.85 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var express = require('express'),
OAuth = require('oauth').OAuth,
querystring = require('querystring');
// Setup the Express.js server
var app = express.createServer();
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
secret: "skjghskdjfhbqigohqdiouk"
}));
// Home Page
app.get('/', function(req, res){
if(!req.session.oauth_access_token) {
res.redirect("/google_login");
}
else {
res.redirect("/google_contacts");
}
});
// Request an OAuth Request Token, and redirects the user to authorize it
app.get('/google_login', function(req, res) {
var getRequestTokenUrl = "https://www.google.com/accounts/OAuthGetRequestToken";
// GData specifid: scopes that wa want access to
var gdataScopes = [
querystring.escape("https://www.google.com/m8/feeds/"),
querystring.escape("https://www.google.com/calendar/feeds/")
];
var oa = new OAuth(getRequestTokenUrl+"?scope="+gdataScopes.join('+'),
"https://www.google.com/accounts/OAuthGetAccessToken",
"anonymous",
"anonymous",
"1.0",
"http://localhost:3000/google_cb"+( req.param('action') && req.param('action') != "" ? "?action="+querystring.escape(req.param('action')) : "" ),
"HMAC-SHA1");
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if(error) {
console.log('error');
console.log(error);
}
else {
// store the tokens in the session
req.session.oa = oa;
req.session.oauth_token = oauth_token;
req.session.oauth_token_secret = oauth_token_secret;
// redirect the user to authorize the token
res.redirect("https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token="+oauth_token);
}
})
});
// Callback for the authorization page
app.get('/google_cb', function(req, res) {
// get the OAuth access token with the 'oauth_verifier' that we received
var oa = new OAuth(req.session.oa._requestUrl,
req.session.oa._accessUrl,
req.session.oa._consumerKey,
req.session.oa._consumerSecret,
req.session.oa._version,
req.session.oa._authorize_callback,
req.session.oa._signatureMethod);
console.log(oa);
oa.getOAuthAccessToken(
req.session.oauth_token,
req.session.oauth_token_secret,
req.param('oauth_verifier'),
function(error, oauth_access_token, oauth_access_token_secret, results2) {
if(error) {
console.log('error');
console.log(error);
}
else {
// store the access token in the session
req.session.oauth_access_token = oauth_access_token;
req.session.oauth_access_token_secret = oauth_access_token_secret;
res.redirect((req.param('action') && req.param('action') != "") ? req.param('action') : "/google_contacts");
}
});
});
function require_google_login(req, res, next) {
if(!req.session.oauth_access_token) {
res.redirect("/google_login?action="+querystring.escape(req.originalUrl));
return;
}
next();
};
app.get('/google_contacts', require_google_login, function(req, res) {
var oa = new OAuth(req.session.oa._requestUrl,
req.session.oa._accessUrl,
req.session.oa._consumerKey,
req.session.oa._consumerSecret,
req.session.oa._version,
req.session.oa._authorize_callback,
req.session.oa._signatureMethod);
console.log(oa);
// Example using GData API v3
// GData Specific Header
oa._headers['GData-Version'] = '3.0';
oa.getProtectedResource(
"https://www.google.com/m8/feeds/contacts/default/full?alt=json",
"GET",
req.session.oauth_access_token,
req.session.oauth_access_token_secret,
function (error, data, response) {
var feed = JSON.parse(data);
res.render('google_contacts.ejs', {
locals: { feed: feed }
});
});
});
app.get('/google_calendars', require_google_login, function(req, res) {
var oa = new OAuth(req.session.oa._requestUrl,
req.session.oa._accessUrl,
req.session.oa._consumerKey,
req.session.oa._consumerSecret,
req.session.oa._version,
req.session.oa._authorize_callback,
req.session.oa._signatureMethod);
// Example using GData API v2
// GData Specific Header
oa._headers['GData-Version'] = '2';
oa.getProtectedResource(
"https://www.google.com/calendar/feeds/default/allcalendars/full?alt=jsonc",
"GET",
req.session.oauth_access_token,
req.session.oauth_access_token_secret,
function (error, data, response) {
var feed = JSON.parse(data);
res.render('google_calendars.ejs', {
locals: { feed: feed }
});
});
});
app.listen(3000);
console.log("listening on http://localhost:3000");