-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.js
63 lines (55 loc) · 1.97 KB
/
authentication.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
const {google} = require('googleapis');
const fs = require('fs');
const readline = require('readline');
const OAuth2Client = google.auth.OAuth2;
const ACCESS_TOKEN_FILE='access_token.json';
module.exports = class Authentication{
// Client ID and client secret are available at
// https://code.google.com/apis/console
constructor(client_id,client_secret,redirect_url,scope){
this.scope=scope;
this.oauth2Client = new OAuth2Client(client_id, client_secret, redirect_url);
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
}
getCachedAccessToken(){
// Load client secrets from a local file.
try {
let access_token=fs.readFileSync(ACCESS_TOKEN_FILE);
return JSON.parse(access_token);
} catch (error) {
return null;
}
}
authorize (callback) {
let tokens=this.getCachedAccessToken();
if(tokens!==null){
//access token cached
this.oauth2Client.setCredentials(tokens);
callback(this.oauth2Client);
} else {
// generate consent page url
const url = this.oauth2Client.generateAuthUrl({
access_type: 'offline', // will return a refresh token
scope: this.scope // can be a space-delimited string or an array of scopes
});
console.log('Visit the url: ', url);
this.rl.question('Enter the code here:', code => {
// request access token
this.rl.close();
this.oauth2Client.getToken(code, (err, tokens) => {
if (err) {
throw new Error(err);
}
// set tokens to the client
// TODO: tokens should be set by OAuth2 client.
this.oauth2Client.setCredentials(tokens);
fs.writeFileSync(ACCESS_TOKEN_FILE, JSON.stringify(tokens));
callback(this.oauth2Client);
});
});
}
}
};