Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets, JWT client with Service Account

I am pulling my hair out ! Help !! UPDATE: I am using v1.0.0 of google-auth-library and v24.0.0 of googleapis.

const { JWT } = require('google-auth-library');
var google = require('googleapis');
var sheets = google.sheets('v4');

const client = new JWT({
  email: keys.client_email
  ,key: keys.private_key
  ,scopes: ['https://spreadsheets.google.com/feeds']
});

return new Promise(function(resolve, reject) {

  client.authorize()
  .then((obj) => {

  // fails at this call
  sheets.spreadsheets.values.append({

    auth: client
    ,range: "A1"
    ,spreadsheetId: SHEET_ID
    ,insertDataOptions: "INSERT_ROWS"
    ,responseDateTimeRenderOption: "FORMATTED_STRING"
    ,responseValueRenderOption: "UNFORMATTED_VALUE"
    ,valueInputOption: "RAW"
    ,resource: {
      values: [
        [1,2,3]
      ]
    }

....... code omitted for clarity

I keep getting:

'json' is not a valid configuration option. Please use 'data' instead. This library is using Axios for requests. Please see https://github.com/axios/axios to learn more about the valid request options. at Object.validate (/user_code/node_modules/google-auth- library/build/src/options.js:32:19) at DefaultTransporter.request (/user_code/node_modules/google-auth-library/build/src/transporters.js:49:23) at JWT. (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:427:63) at step (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:57:23) at Object.next (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:38:53) at fulfilled (/user_code/node_modules/google-auth-library/build/src/auth/oauth2client.js:29:58) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

like image 863
jldupont Avatar asked Jan 13 '18 18:01

jldupont


1 Answers

I solved it by following https://www.npmjs.com/package/googleapis and not using google-auth-library at all.

var google = require('googleapis');
const client = new google.auth.JWT(
    keys.client_email,
    null,
    keys.private_key,
    ['https://www.googleapis.com/auth/spreadsheets'],
    null
);
return new Promise((resolve, reject) => {
    client.authorize((err, tokens) => {
        if (err) {
            reject(err);
        } else {
            google.options({
                auth: client
            });
            resolve();
        }
    });
});

//and then use client as you did
like image 125
Stefan Henze Avatar answered Oct 18 '22 19:10

Stefan Henze