forked from clegallic/MMM-GoogleDriveSlideShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-auth-token.js
50 lines (44 loc) · 1.74 KB
/
generate-auth-token.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
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive.readonly'];
const SECRETS_PATH = './secrets';
const TOKEN_PATH = `${SECRETS_PATH}/token.json`;
const CREDENTIALS_PATH = `${SECRETS_PATH}/credentials.json`;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Check credentialsDir exists, create it if necessary
if (!fs.existsSync(SECRETS_PATH)){
console.log(`${SECRETS_PATH} folder does not exists, creating it.`);
fs.mkdirSync(SECRETS_PATH);
}
if (!fs.existsSync(CREDENTIALS_PATH)){
console.error(`${CREDENTIALS_PATH} file does not exists. Follow README.md instructions to create it`);
process.exit(0);
}
// Load client secrets from a local file.
fs.readFile(CREDENTIALS_PATH, (err, content) => {
if (err) return console.error('Error loading the client secret file:', err);
const credentials = JSON.parse(content);
// Create OAuth2 client and authorize
const { client_secret, client_id, redirect_uris } = credentials.installed;
const OAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
const authUrl = OAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize the app by visiting this url:', authUrl);
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
OAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token: ', err);
// store the token on disk
fs.writeFile(TOKEN_PATH, JSON.stringify(token), err => {
if (err) console.error(err);
console.log('Token successfully stored');
})
})
})
});