-
Notifications
You must be signed in to change notification settings - Fork 69
/
sheetsSetup.js
57 lines (53 loc) · 1.93 KB
/
sheetsSetup.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
require('dotenv').config();
const readline = require('readline');
const { google } = require('googleapis');
const { OAuth2Client } = require('google-auth-library');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {Function} callback The callback for the authorized client.
*/
const getNewToken = (oAuth2Client, callback) => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) {
return console.error(
'Error while trying to retrieve access token', err
);
}
oAuth2Client.setCredentials(token);
callback(token);
});
});
};
(function() {
const credentials = JSON.parse(process.env.CLIENT_SECRET);
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);
getNewToken(oauth2Client, function(token) {
// Logs new sheets access token.
console.log('');
console.log('Access Token: ', JSON.stringify(token));
console.log('');
console.log(
'Add the newly generated access token to the CREDENTIALS variable in ' +
'the .env'
);
});
})();