-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
45 lines (39 loc) · 1.17 KB
/
auth.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
'use strict';
require('dotenv').config();
const auth = require('./modules/auth');
const opn = require('opn');
const express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('dev'));
const scopes = ['https://www.googleapis.com/auth/script.external_request'];
app.get('/oauthcallback', function(req, res) {
const { code } = req.query;
console.log('this is sparta');
return auth.getToken(code, function(err, tokens) {
if (err) {
return res.status(500).send(err.toString());
}
console.log(tokens);
auth.setCredentials(tokens);
res.status(200).send(`
<h1>Credentials retrieved successfully</h1>
<h2>Add following lines to your .env</2>
<pre>
ACCESS_TOKEN="${tokens.access_token}"
REFRESH_TOKEN="${tokens.refresh_token}"
</pre>
`);
process.exit();
});
});
app.listen(3000, function() {
console.log('Listening on 3000...');
const url = auth.generateAuthUrl({ scope: scopes, access_type: 'offline' });
console.log(`
To authorize this client please open the following URL in your browser and follow instructions:
${url}
`);
// Open the URL in the default browser
opn(url);
});