theoephraim / node-google-spreadsheet

Google Sheets API wrapper for Javascript / Typescript
https://theoephraim.github.io/node-google-spreadsheet
The Unlicense
2.34k stars 390 forks source link

Creating a spreadsheet document with the JWT causes an error #701

Closed Ivokato closed 2 months ago

Ivokato commented 3 months ago

After reinstalling my node modules I got this error: Cannot use api key only to create a new spreadsheet - it is only usable for read-only access of public docs

My bare minimum code taken from the official documentation is failing:

import {JWT} from 'google-auth-library'
import {GoogleSpreadsheet} from 'google-spreadsheet';

// I put key redacted here in the issue but used real private key in my code
const key = '-----BEGIN PRIVATE KEY-----(key redacted)-----END PRIVATE KEY-----\n';

const SCOPES = [
  'https://www.googleapis.com/auth/spreadsheets',
  'https://www.googleapis.com/auth/drive.file',
];

const jwt = new JWT({
  email: 'agent@company.iam.gserviceaccount.com',
  key,
  scopes: SCOPES,
});

GoogleSpreadsheet.createNewSpreadsheetDocument(jwt, {title: 'hello'}))
.then(result => {
  console.log('has result!: ', !!result);
}).catch(error => {
  console.log(error.message);
});

I figured out that the error is thrown because there is a key called "apiKey" in the JWT. Downgrading the google-auth-library to 9.13.0 has solved the issue for me so I would recommend either fixing the dependency or updating the check that throws the error

niclake commented 3 months ago

I don't have much to offer, but curiously, have you tried key: key, inside defining your JWT?

Sma1lboy commented 2 months ago

I met same issue with official documentation code Cannot use api key only to create a new spreadsheet - it is only usable for read-only access of public docs Seems No permission from service account to create spreadsheet

theoephraim commented 2 months ago

fix incoming shortly!