-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (57 loc) · 1.5 KB
/
server.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
const gsheets = require ('./googlesheets');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(cors());
app.listen(port, () => {
console.log('Listening on http://localhost:' + port);
});
app.get('/auth/step1', (req, res) => {
gsheets.authorize({
client_id: req.headers['client_id'],
client_secret: req.headers['client_secret']
}, (oAuth) => {
res.send(gsheets.getAuthUrl(oAuth));
});
})
app.get('/auth/step2', (req, res) => {
gsheets.authorize({
client_id: req.headers['client_id'],
client_secret: req.headers['client_secret']
}, (oAuth) => {
gsheets.getToken(oAuth, req.query.code, (token, error) => {
res.send({
token,
error
})
})
});
})
app.post('/table/get', (req, res) => {
gsheets.getDataAuthed({
client_id: req.headers['client_id'],
client_secret: req.headers['client_secret'],
token: JSON.parse(req.headers['token'])
}, {
spreadsheetId: req.body.spreadsheetId,
range: req.body.sheetTab + '!' + req.body.range
}, (data) => {
res.send(data.values);
});
})
app.post('/table/update', (req, res) => {
gsheets.updateDataAuthed({
client_id: req.headers['client_id'],
client_secret: req.headers['client_secret'],
token: JSON.parse(req.headers['token'])
}, {
spreadsheetId: req.body.spreadsheetId,
range: req.body.sheetTab + '!' + req.body.range,
values: req.body.values
}, (data) => {
res.send(data);
});
})