forked from Errorname/google-docs-mustaches
-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.js
86 lines (74 loc) · 2.32 KB
/
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
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
86
const fs = require('fs')
const readline = require('readline')
const { google } = require('googleapis')
const credentials = require('./credentials.json')
const SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/documents'
]
const TOKEN_PATH = 'dev/token.json'
const readFile = path =>
new Promise((resolve, reject) =>
fs.readFile(path, (err, content) => {
if (err) return reject(err)
resolve(JSON.parse(content))
})
)
const writeFile = (path, json) =>
new Promise((resolve, reject) =>
fs.writeFile(path, JSON.stringify(json), err => {
if (err) return reject(err)
resolve()
})
)
const askQuestion = text =>
new Promise(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(text, code => {
rl.close()
resolve(code)
})
})
const getNewToken = oAuth2Client =>
new Promise(async resolve => {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
})
console.log('Authorize this app by visiting this url:', authUrl)
const code = await askQuestion('Enter the code from that page here: ')
oAuth2Client.getToken(code, async (err, token) => {
if (err) return console.error('Error retrieving access token', err)
console.log(token)
oAuth2Client.setCredentials(token)
await writeFile(TOKEN_PATH, token)
console.log('Token stored to', TOKEN_PATH)
resolve(token)
})
})
const refreshToken = (oAuth2Client, token) =>
new Promise(async resolve => {
oAuth2Client.setCredentials(token)
const accessToken = (await oAuth2Client.getRequestHeaders()).Authorization.split(' ')[1]
token.access_token = accessToken
await writeFile(TOKEN_PATH, token)
console.log('Token refreshed')
resolve()
})
;(async () => {
const { client_secret, client_id, redirect_uris } = credentials.installed
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0])
let token
try {
token = await readFile(TOKEN_PATH)
oAuth2Client.setCredentials(token)
refreshToken(oAuth2Client, token)
} catch (err) {
console.error(err)
token = await getNewToken(oAuth2Client)
}
console.log('\nYou can now use the playground: npm run playground')
})()