forked from cofacts/rumors-line-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthGoogleDrive.js
82 lines (77 loc) · 2.37 KB
/
authGoogleDrive.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
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/drive.file'];
var ENV_FILE_PATH = '.env';
let secrets;
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Drive API.
secrets = JSON.parse(content);
authorize(secrets);
});
/**
* Create an OAuth2 client with the given credentials.
*
* @param {Object} credentials The authorization client credentials.
*/
function authorize(credentials) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
getNewToken(oauth2Client);
}
/**
* Get and store new token after prompting for user authorization.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
*/
function getNewToken(oauth2Client) {
var authUrl = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
storeToken(token);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken(token) {
fs.appendFile(
ENV_FILE_PATH,
`GOOGLE_CREDENTIALS=${JSON.stringify({ token, secrets })}`,
function(err) {
if (err) {
console.log('Error while trying to store access token', err);
} else {
console.log('Token appended to ' + ENV_FILE_PATH);
}
}
);
}