-
Notifications
You must be signed in to change notification settings - Fork 2
/
googleHelper.js
169 lines (156 loc) · 4.29 KB
/
googleHelper.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint no-console: 0 */
const ConfigStore = require('configstore')
const pkg = require('./package.json')
const http = require('http')
const url = require('url')
const querystring = require('querystring')
const opn = require('opn')
const { google } = require('googleapis')
const OAuth2Client = google.auth.OAuth2
const chalk = require('chalk')
const clear = require('clear')
const figlet = require('figlet')
const SCOPES = [
'https://www.googleapis.com/auth/contacts',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
]
function getGoogleCode(oAuth2Client) {
return new Promise((resolve, reject) => {
// Open an http server to accept the oauth callback. In this simple example, the
// only request to our webserver is to /oauth2callback?code=<code>
const server = http
.createServer(async (req, res) => {
if (req.url.indexOf('/oauth2callback') > -1) {
// acquire the code from the querystring, and close the web server.
const { code } = querystring.parse(url.parse(req.url).query)
res.end(
`Authentication successful! Please return to the console. [code: ${code}]`
)
server.close()
resolve(code)
}
reject(new Error('oops', req, res))
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
// Generate the url that will be used for the consent dialog.
const authorizeUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
})
opn(authorizeUrl)
})
})
}
function setupNewOAuth2Client({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET
}) {
const REDIRECT_URL = 'http://localhost:3000/oauth2callback'
return new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL)
}
async function getTokens(oAuth2Client) {
const conf = new ConfigStore(pkg.name)
const storedTokens = conf.get('google.tokens')
if (storedTokens) {
console.log(
chalk.green(
'Found token in your config file. If you want to reset it, run with `--reset`.'
)
)
return storedTokens
}
console.log(
chalk.green(
'Authenticating you, check out your browser to fill the form out…'
)
)
const newTokens = await getGoogleCode(oAuth2Client)
.then(code => oAuth2Client.getToken(code))
.then(res => res.tokens)
conf.set('google.tokens', newTokens)
return newTokens
}
function resetConfigStore() {
const conf = new ConfigStore(pkg.name)
conf.delete('google.tokens')
}
function getFileContent(configFilename) {
try {
return require(`./${configFilename}`)
} catch (err) {
return {}
}
}
function getKeys() {
try {
return require('./keys.json')
} catch (ex) {
console.log(
chalk.red(
'Unable to retrieve CLIENT_ID nor CLIENT_SECRET, please follow documentation to update keys.json file.'
)
)
process.exit(-1)
}
}
function getAccountInfo(oAuthClient) {
const peopleAPI = google.people({
version: 'v1',
auth: oAuthClient
})
return new Promise((resolve, reject) => {
peopleAPI.people.get(
{
resourceName: 'people/me',
personFields: 'emailAddresses'
},
(err, res) => {
if (err) {
return reject(err)
}
resolve(res.data)
}
)
})
}
clear()
console.log(
chalk.yellow(
figlet.textSync('Google API Helper', { horizontalLayout: 'full' })
)
)
const KONNECTOR_DEV_CONFIG_FILE = 'konnector-dev-config.json'
const run = async () => {
const {
reset,
filename: configFilename = KONNECTOR_DEV_CONFIG_FILE
} = require('minimist')(process.argv.slice(2))
reset && resetConfigStore()
const keys = getKeys()
const oAuthClient = await setupNewOAuth2Client(keys)
const tokens = await getTokens(oAuthClient)
require('fs').writeFileSync(
`./${configFilename}`,
JSON.stringify(
{
...getFileContent(configFilename),
fields: { ...tokens }
},
null,
2
)
)
oAuthClient.setCredentials(tokens)
const accountInfo = await getAccountInfo(oAuthClient)
console.log(
chalk.green(
`Find your credentials in ${configFilename} for ${
accountInfo['emailAddresses'][0]['value']
}`
)
)
process.exit()
}
run()