-
Notifications
You must be signed in to change notification settings - Fork 1
/
getFreshToken.js
36 lines (35 loc) · 1.18 KB
/
getFreshToken.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
var request = require('request');
module.exports = getFreshToken;
var params = {
client_id: process.env.GAPI_KEY,
client_secret: process.env.GAPI_SECRET,
grant_type: 'refresh_token'
};
var tokenInfo = {expires: 0, pending: []};
function getFreshToken(refreshToken, done) {
if (tokenInfo.expires > Date.now() + 60000) return done(null, tokenInfo.access_token);
tokenInfo.pending.push(done);
if (tokenInfo.pending.length > 1) return;
var form = JSON.parse(JSON.stringify(params));
form.refresh_token = refreshToken;
request.post({url: 'https://accounts.google.com/o/oauth2/token', form: form}, onResponse);
function onResponse(err, response, body) {
if (err) return complete(err);
try {
var info = JSON.parse(body);
if (info.error) return complete(info.error);
tokenInfo.access_token = info.access_token;
tokenInfo.expires = Date.now() + 1000 * +info.expires_in;
return complete(null, tokenInfo.access_token);
} catch (e) {
return complete(e);
}
}
function complete(err, result) {
var cbs = tokenInfo.pending;
tokenInfo.pending = [];
cbs.forEach(function (cb) {
try { cb(err, result); } catch (e) {}
});
}
}