forked from smurthas/gdata-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
69 lines (58 loc) · 2.07 KB
/
test.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
var request = require('request');
var querystring = require('querystring');
var fs = require('fs');
var clientID = process.argv[2];
var clientSecret = process.argv[3];
if (! (clientID && clientSecret) ) {
console.error('usage: node test.js <clientID> <clientSecret>');
process.exit(1);
}
// get an clientID and clientSecret at https://code.google.com/apis/console/
var gdataClient = require('./gdata')(clientID, clientSecret, 'http://localhost:8553/');
var scope = 'https://www.google.com/m8/feeds/'; //contacts
var express = require('express'),
connect = require('connect'),
app = express();
app.use(connect.bodyParser());
var token;
app.get('/', function (req, res) {
// see https://developers.google.com/accounts/docs/OAuth2WebServer for options
gdataClient.getAccessToken({scope: scope,
access_type: 'offline',
approval_prompt: 'force'}, req, res, function(err, _token) {
if(err) {
console.error('oh noes!', err);
res.writeHead(500);
res.end('error: ' + JSON.stringify(err));
} else {
token = _token;
console.log('got token:', token);
res.redirect('/getStuff');
}
});
});
app.get('/getStuff', function(req, res) {
gdataClient.getFeed('https://www.google.com/m8/feeds/contacts/default/full', {'max-results':3},
function(err, feed) {
res.writeHead(200);
for(var i in feed.feed.entry) {
res.write(JSON.stringify(feed.feed.entry[i].title));
res.write(JSON.stringify(feed.feed.entry[i].gd$email));
res.write('\n\n');
}
res.end();
});
});
app.get('/refresh', function(req, res) {
console.log('forcing refresh...');
gdataClient._refreshToken(function(err, result) {
console.log('err,', err);
console.log('result,', result);
console.log('token,', token);
});
});
gdataClient.on('tokenRefresh', function() {
console.log('token refresh!', token);
});
app.listen(process.argv[4] || 8553);
console.log('open http://localhost:8553');