-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgoogle_auth.js
More file actions
56 lines (45 loc) · 1.53 KB
/
google_auth.js
File metadata and controls
56 lines (45 loc) · 1.53 KB
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
require('dotenv').config()
const app = require('express')()
const { google } = require('googleapis')
const key = process.argv[ 2 ]
if (!key) {
console.error('Please provide key name as first argument')
process.exit(1)
}
const clientId = process.env?.[ key + 'clientId' ]
const clientSecret = process.env?.[ key + 'clientSecret' ]
const authHost = process.env?.[ 'AUTH_authHost' ]
const authPort = process.env?.[ 'AUTH_authPort' ]
if (!clientId || !clientSecret || !authHost) {
console.error('Please provide information for auth in .env file')
process.exit(1)
}
const authUrl = `${authHost}:${authPort}`
const oauth2Client = new google.auth.OAuth2(clientId, clientSecret, authUrl + '/auth')
app.get('/', (req, res) => {
const url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: [
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.events',
],
})
console.log(`
Or you can open the following URL in your browser:
${url}
`)
res.writeHead(302, { Location: url })
res.end()
})
app.get('/auth', async (req, res) => {
const { code } = req.query
const { tokens } = await oauth2Client.getToken(code)
const message = `Your refresh token is: "${tokens.refresh_token}" \nPlease add it to your .env file as "${key}refreshToken".`
console.log(message)
res.send(message)
res.end()
process.exit(0)
})
app.listen(authPort)
console.log(`Server listening on ${authUrl}, When the browser is not opened, open it manually`)
import('open').then(open => open.default(authUrl))