forked from BilingualAphasia/BilingualAphasiaTestScorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdata.js
138 lines (127 loc) · 5.22 KB
/
gdata.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
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
var querystring = require('querystring');
var https = require('https');
var EventEmitter = require('events').EventEmitter;
var URL = require('url');
var oauthBase = 'https://accounts.google.com/o/oauth2';
module.exports = function(client_id, client_secret, redirect_uri) {
var clientID = client_id;
var clientSecret = client_secret;
var redirectURI = redirect_uri;
var token;
var client = new EventEmitter();
client.getAccessToken = function(scope, req, res, callback) {
if(req.query.error) {
callback(req.query.error);
} else if(!req.query.code) {
var height = 750;
var width = 980;
resp = "<script type='text/javascript'>var left= (screen.width / 2) - (" + width + " / 2); var top = (screen.height / 2) - (" + height + " / 2); window.open('" + oauthBase + '/auth?' + querystring.stringify({client_id: clientID, redirect_uri: redirectURI, scope: scope, response_type: 'code'}) + "', 'auth', 'menubar=no,toolbar=no,status=no,width=" + width + ",height=" + height + ",toolbar=no,left=' + left + 'top=' + top);</script>";
res.end(resp + '<a target=_new href=\'' + oauthBase + '/auth?' + querystring.stringify({client_id: clientID ,
redirect_uri: redirectURI,
scope: scope,
response_type: 'code'}) + '\'>Authenticate</a>');
} else {
doPost({grant_type:'authorization_code',
code:req.query.code,
client_id:clientID,
client_secret:clientSecret,
redirect_uri:redirectURI}, function(err, tkn) {
if(!err && tkn && !tkn.error)
token = tkn;
callback(err, tkn);
});
}
}
client.setToken = function(tkn) {
token = tkn;
}
client.getFeed = function(url, params, callback) {
if(!callback && typeof params === 'function') {
callback = params;
params = {};
}
params.oauth_token = token.access_token;
params.alt = 'json';
var reqUrl = url + '?' + querystring.stringify(params);
doRequest(url, params, function(err, body) {
callback(err, body);
});
};
function doRequest(url, params, callback) {
var path = URL.parse(url).pathname + '?' + querystring.stringify(params);
var options = {
host: 'www.google.com',
port: 443,
path: path,
method: 'GET'
};
var httpsReq = https.request(options, function(httpsRes) {
if(httpsRes.statusCode === 401) {
refreshToken(function(err, result) {
if(!err && result && !result.error && result.access_token) {
token.access_token = result.access_token;
token.refresh_token = result.refresh_token;
client.emit('tokenRefresh');
client.getFeed(url, params, callback);
}
});
} else {
var data = '';
httpsRes.on('data', function(moreData) {
data += moreData;
});
httpsRes.on('end', function() {
try {
callback(null, JSON.parse(data.toString()));
} catch(err) {
callback(err, null);
}
})
}
});
httpsReq.on('error', function(e) {
callback(e, null);
});
httpsReq.end();
}
function refreshToken(callback) {
doPost({client_id:clientID,
client_secret:clientSecret,
refresh_token:token.refresh_token,
grant_type:'refresh_token'
}, function(err, result) {
if(err || !result || !result.access_token) {
console.error('err', err);
console.error('result', result);
}
callback(err, result);
});
}
return client;
}
function doPost(body, callback) {
var options = {
host: 'accounts.google.com',
port: 443,
path: '/o/oauth2/token',
method: 'POST',
headers: {'Content-Type':'application/x-www-form-urlencoded'}
};
var httpsReq = https.request(options, function(httpsRes) {
if(httpsRes.statusCode === 200) {
httpsRes.on('data', function(data) {
callback(null, JSON.parse(data.toString()));
});
} else {
httpsRes.on('data', function(data) {
console.error("refreshing token -- statusCode !== 200, yoikes! data:", data.toString());
callback(data.toString());
});
}
});
httpsReq.write(querystring.stringify(body));
httpsReq.on('error', function(e) {
callback(e, null);
});
httpsReq.end();
}