Funkcionalna radna verzija
This commit is contained in:
123
backend/google_api.js
Normal file
123
backend/google_api.js
Normal file
@@ -0,0 +1,123 @@
|
||||
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.split('/')[5];
|
||||
const PAIRS_LIST = process.env.PAIRS_LIST.split('/')[5];
|
||||
|
||||
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';
|
||||
|
||||
var oauth2Client = null;
|
||||
|
||||
|
||||
function executeAPI(callback){
|
||||
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.
|
||||
return authorize(JSON.parse(content), callback);
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
return callback();
|
||||
} else {
|
||||
oauth2Client.credentials = JSON.parse(token);
|
||||
var resultat = callback();
|
||||
console.log("resultat");
|
||||
console.log(resultat);
|
||||
//return callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getNewToken() {
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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 getPairs(){
|
||||
auth=oauth2Client;
|
||||
ranges = ['2017!A:A','2017!B:B']; //get all rows with names
|
||||
var sheets = google.sheets('v4');
|
||||
sheets.spreadsheets.values.batchGet({
|
||||
auth: auth,
|
||||
spreadsheetId: PAIRS_LIST,
|
||||
ranges: ranges
|
||||
}, function(err, result) {
|
||||
if(err) {
|
||||
// Handle error
|
||||
console.log(err);
|
||||
return null;
|
||||
} else {
|
||||
const pairs = [];
|
||||
|
||||
for (let i=0;i<result.valueRanges[0].values.length;i++){
|
||||
let name1 = result.valueRanges[0].values[i][0];
|
||||
if (name1){
|
||||
let name2 = result.valueRanges[1].values[i][0];
|
||||
pairs.push({name1:name1,name2:name2});
|
||||
}
|
||||
}
|
||||
console.log(pairs);
|
||||
return pairs;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getPairs: function{executeAPI(getPairs(callback));}
|
||||
}
|
||||
Reference in New Issue
Block a user