/
quickstart.js
92 lines (85 loc) · 2.79 KB
/
quickstart.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
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
// [START main_body]
const {google} = require('googleapis');
const express = require('express');
const opn = require('open');
const path = require('path');
const fs = require('fs');
const keyfile = path.join(__dirname, 'credentials.json');
const keys = JSON.parse(fs.readFileSync(keyfile));
const scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
// Create an oAuth2 client to authorize the API call
const client = new google.auth.OAuth2(
keys.web.client_id,
keys.web.client_secret,
keys.web.redirect_uris[0]
);
// Generate the url that will be used for authorization
this.authorizeUrl = client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
});
// 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 app = express();
app.get('/oauth2callback', (req, res) => {
const code = req.query.code;
client.getToken(code, (err, tokens) => {
if (err) {
console.error('Error getting oAuth tokens:');
throw err;
}
client.credentials = tokens;
res.send('Authentication successful! Please return to the console.');
server.close();
listMajors(client);
});
});
const server = app.listen(3000, () => {
// open the browser to the authorize url to start the workflow
opn(this.authorizeUrl, {wait: false});
});
/**
* Print the names and majors of students in a sample spreadsheet:
* https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
*/
function listMajors(auth) {
const sheets = google.sheets('v4');
sheets.spreadsheets.values.get(
{
auth: auth,
spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms',
range: 'Class Data!A2:E',
},
(err, res) => {
if (err) {
console.error('The API returned an error.');
throw err;
}
const rows = res.data.values;
if (rows.length === 0) {
console.log('No data found.');
} else {
console.log('Name, Major:');
for (const row of rows) {
// Print columns A and E, which correspond to indices 0 and 4.
console.log(`${row[0]}, ${row[4]}`);
}
}
}
);
}
// [END main_body]