-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticateToken.js
46 lines (39 loc) · 1.39 KB
/
authenticateToken.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
/* jshint esversion: 6*/
var fs = require('fs');
const {google} = require('googleapis');
function getAuthorizationToken(code,cb) {
fs.readFile('authKeys.json',function(err,data) {
if(err) {
return cb(err);
}
var credentials = JSON.parse(data);
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var oauth2Client = new google.auth.OAuth2(clientId, clientSecret, redirectUrl);
oauth2Client.getToken(code, function(err, token){
if(err) {
return cb(err);
}
var credentialsFile = 'authenticationTokenFile.json';
fs.writeFile(credentialsFile, JSON.stringify(token), (err)=> {
if (err) return console.error(err);
console.log('Token stored to', credentialsFile);
});
return cb(null, credentialsFile);
});
});
}
if(process.argv.length!=3) {
console.log('usage: node get_token token');
process.exit(1);
}
var token = process.argv[2];
getAuthorizationToken(token, function(err,credentialsFile) {
if(err) {
console.log('err:',err);
}
else {
console.log('Authorization token is in: \n',credentialsFile);
}
});