├── .gitignore ├── package.json ├── client_secret.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "abcd", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node index" 9 | }, 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "fs": "0.0.1-security", 14 | "google-auth-library": "^0.12.0", 15 | "googleapis": "^26.0.1", 16 | "readline": "^1.3.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /client_secret.json: -------------------------------------------------------------------------------- 1 | { 2 | "installed": { 3 | "client_id": "-.apps.googleusercontent.com", 4 | "project_id": "", 5 | "auth_uri": "https://accounts.google.com/o/oauth2/auth", 6 | "token_uri": "https://accounts.google.com/o/oauth2/token", 7 | "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 8 | "client_secret": "", 9 | "redirect_uris": [ 10 | "urn:ietf:wg:oauth:2.0:oob", 11 | "http://localhost" 12 | ] 13 | } 14 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | var fs = require('fs'); 4 | var readline = require('readline'); 5 | var { google } = require('googleapis'); 6 | // var googleAuth 7 | const GoogleAuth = require('google-auth-library'); 8 | 9 | // If modifying these scopes, delete your previously saved credentials 10 | // at ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json 11 | var SCOPES = ['https://www.googleapis.com/auth/spreadsheets']; 12 | var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || 13 | process.env.USERPROFILE) + '/.credentials/'; 14 | var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json'; 15 | 16 | // Load client secrets from a local file. 17 | fs.readFile('client_secret.json', function processClientSecrets(err, content) { 18 | if (err) { 19 | console.log('Error loading client secret file: ' + err); 20 | return; 21 | } 22 | // Authorize a client with the loaded credentials, then call the 23 | // Google Sheets API. 24 | authorize(JSON.parse(content), listMajors); 25 | }); 26 | 27 | /** 28 | * Create an OAuth2 client with the given credentials, and then execute the 29 | * given callback function. 30 | * 31 | */ 32 | function authorize(credentials, callback) { 33 | var clientSecret = credentials.installed.client_secret; 34 | var clientId = credentials.installed.client_id; 35 | var redirectUrl = credentials.installed.redirect_uris[0]; 36 | var auth = new GoogleAuth(); 37 | var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 38 | 39 | // Check if we have previously stored a token. 40 | fs.readFile(TOKEN_PATH, function (err, token) { 41 | if (err) { 42 | getNewToken(oauth2Client, callback); 43 | } else { 44 | oauth2Client.credentials = JSON.parse(token); 45 | callback(oauth2Client); 46 | } 47 | }); 48 | } 49 | 50 | /** 51 | * Get and store new token after prompting for user authorization, and then 52 | * execute the given callback with the authorized OAuth2 client. 53 | * client. 54 | */ 55 | function getNewToken(oauth2Client, callback) { 56 | var authUrl = oauth2Client.generateAuthUrl({ 57 | access_type: 'offline', 58 | scope: SCOPES 59 | }); 60 | console.log('Authorize this app by visiting this url: ', authUrl); 61 | var rl = readline.createInterface({ 62 | input: process.stdin, 63 | output: process.stdout 64 | }); 65 | rl.question('Enter the code from that page here: ', function (code) { 66 | rl.close(); 67 | oauth2Client.getToken(code, function (err, token) { 68 | if (err) { 69 | console.log('Error while trying to retrieve access token', err); 70 | return; 71 | } 72 | oauth2Client.credentials = token; 73 | storeToken(token); 74 | callback(oauth2Client); 75 | }); 76 | }); 77 | } 78 | 79 | /** 80 | * Store token to disk be used in later program executions. 81 | * 82 | * @param {Object} token The token to store to disk. 83 | */ 84 | function storeToken(token) { 85 | try { 86 | fs.mkdirSync(TOKEN_DIR); 87 | } catch (err) { 88 | if (err.code != 'EEXIST') { 89 | throw err; 90 | } 91 | } 92 | fs.writeFile(TOKEN_PATH, JSON.stringify(token)); 93 | console.log('Token stored to ' + TOKEN_PATH); 94 | } 95 | 96 | /** 97 | * Print the names and majors of students in a sample spreadsheet: 98 | * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit 99 | */ 100 | function listMajors(auth) { 101 | var sheets = google.sheets('v4'); 102 | 103 | 104 | sheets.spreadsheets.values.get({ 105 | auth: auth, 106 | spreadsheetId: "1LBkRSiEhr1gS28VNDMq6WjWkPhGRKB0lQrKNp25CIoc", 107 | range: 'Class Data' 108 | }, function (err, result) { 109 | if (err) { 110 | console.log('The API returned an error: ' + err); 111 | return; 112 | } else { 113 | // var numRows = result.values ? result.values.length : 0; 114 | console.log('rows retrieved.', result); 115 | } 116 | }); 117 | } 118 | --------------------------------------------------------------------------------