Files
old-slucajna-televizija/backend/app.js
GotPPay a75ea978f9 ..
2017-10-16 20:21:19 +02:00

149 lines
4.0 KiB
JavaScript

const express = require('express')
const bodyParser = require("body-parser");
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
require('dotenv').config()
const PEOPLE_DB = process.env.PEOPLE_DB;
const PAIRS_LIST = process.env.PAIRS_LIST;
const app = express()
const router = express.Router();
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());
/*
app.get('/', function (req, res) {
res.send('Hello World!')
})
*/
var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json';
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
console.log("reading client secret");
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Google Sheets API.
authorize(JSON.parse(content), listPairs);
});
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
console.log(TOKEN_PATH);
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
console.log(JSON.stringify(token));
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
function listPairs(auth){
ranges = ['2017!A:A','2017!B:B'];
var sheets = google.sheets('v4');
sheets.spreadsheets.values.batchGet({
auth: auth,
spreadsheetId: '1gPuRhTry3YoJ_ibI2BfL1ue2_YLlhtW56OP39ADJY_4',
ranges: ranges
}, function(err, result) {
if(err) {
// Handle error
console.log(err);
} else {
console.log('%d ranges retrieved.', result.valueRanges.length);
console.log(result.valueRanges[0].values[0]);
}
});
}
/*
function listPairs(auth) {
var sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: '1gPuRhTry3YoJ_ibI2BfL1ue2_YLlhtW56OP39ADJY_4',
range: '2017!A:A',
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var rows = response.values;
if (rows.length == 0) {
console.log('No data found.');
} else {
console.log('Name, Major:');
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
// Print columns A and E, which correspond to indices 0 and 4.
console.log('%s, %s', row[0], row[4]);
}
}
});
}*/
router.post('/getPairs',(req,resp)=>{
//resp.send({ime:'Bilal',prezime:'Catic'});
// Load client secrets from a local file.
});
app.listen(3005, function () {
console.log('server na portu 3005');
})