node.js - Authenticating Google Sheet API on Heroku using NodeJS -
i'm following example access google sheets api:
https://developers.google.com/sheets/api/quickstart/nodejs
within example code following method fetch new oauth token.
function getnewtoken(oauth2client, callback) { var authurl = oauth2client.generateauthurl({ access_type: 'offline', scope: scopes }); console.log('authorize app visiting url: ', authurl); var rl = readline.createinterface({ input: process.stdin, output: process.stdout }); rl.question('enter code page here: ', function(code) { rl.close(); oauth2client.gettoken(code, function(err, token) { if (err) { console.log('error while trying retrieve access token', err); return; } oauth2client.credentials = token; storetoken(token); callback(oauth2client); }); }); }
this works fine on local machine (manually visiting page prompted in terminal, , entering code in command line). seems unpractical , doesn't work on heroku. there way automate this? maybe fetching url (and token) in nodejs application , storing somehow?
thanks in advance.
ok, ended using service account key can generated @ https://console.developers.google.com. generate json file of need 2 values: private_key
, client_email
.
to test locally can download dotenv npm module allow store environment variables in .env
file in project root. .env
file this:
google_private_key=<your-key-here-withouth-quotes> google_client_email=<your-email-here-withouth-quotes>
don't forget add .env file .gitignore list when deploying heroku app via git.
my auth.js
file looks this:
const googleauth = require('google-auth-library'); const scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']; function authorize() { return new promise(resolve => { const authfactory = new googleauth(); const jwtclient = new authfactory.jwt( process.env.google_client_email, null, process.env.google_private_key.replace(/\\n/g, '\n'), scopes ); jwtclient.authorize(() => resolve(jwtclient)); }); } module.exports = { authorize, }
note replace function behind private key variable.
my app.js (main file) looks this:
require('dotenv').config(); const google = require('googleapis'); const sheetsapi = google.sheets('v4'); const googleauth = require('./auth'); const spreadsheet_id = 'your-spreadsheet-id'; googleauth.authorize() .then((auth) => { sheetsapi.spreadsheets.values.get({ auth: auth, spreadsheetid: spreadsheet_id, range: "'tab name'!a1:h300", }, function (err, response) { if (err) { console.log('the api returned error: ' + err); return console.log(err); } var rows = response.values; console.log(null, rows); }); }) .catch((err) => { console.log('auth error', err); });
if following error:
the api returned error: error: caller not have permission
share spreadsheet trying load google_client_email , try again.
if works locally, add environment variables heroku app visiting heroku account , going settings
, click reveal config vars
, deploy application. if goes well, should have access document.
Comments
Post a Comment