-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfin_bills_server.mjs
86 lines (78 loc) · 2.94 KB
/
sfin_bills_server.mjs
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
83
84
85
import { listSpecificModified } from './file_loader.mjs'
import {createServer} from 'http'
import {readFile, writeFile} from 'fs'
import {createInterface} from 'readline'
import { google } from 'googleapis'
// const listSpecificModified = fileloader.listSpecificModified;
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const CRED_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'
const TOKEN_PATH = CRED_DIR + 'token.json';
var web_server = createServer(function (request, response) {
// Load client secrets from a local file.
readFile(CRED_DIR + 'client_secret.json', (err, content) => {
if (err) {
console.log('[ERROR] Error loading client secret file:', err);
process.exit(1);
}
// Authorize a client with credentials, then call the Google Sheets API.
authorize(JSON.parse(content), listSpecificModified);
});
response.end()
});
web_server.listen(8321)
console.log("Web server is up")
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
// const auth = new google.auth();
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
// Check if we have previously stored a token.
readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
/**
* 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 {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = 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) {
console.error('[ERROR] Error while trying to retrieve access token', err);
process.exit(1);
}
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('[INFO] Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}