I have a google sheet and I am able to read from google sheet by giving access to service account. Below is the snippet of code for reading.
// configure a JWT auth client
let jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive']);
//authenticate request
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log("Successfully connected!");
}
})
// getting data from google sheet
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: range
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' err);
} else {
console.log('Users list from Google Sheets:', response.data.values);
}
});
this works perfectly, and i am getting the data. But when I want to write to google sheet, it gives an error. Snippet of writing
// writing to google sheets
let values = [
[
'abc','b.ed'
],
];
const requestBody = {
values,
};
sheets.spreadsheets.values.update({
spreadsheetId,
range:"Sheet1!C3",
valueInputOption,
requestBody,
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
} else {
console.log('%d cells updated.', result.updatedCells);
}
});
The error I am getting
GaxiosError: Login Required. at Gaxios._request (/home/spread sheet auth (node js service account)/node_modules/gaxios/build/src/gaxios.js:129:23) at processTicksAndRejections (node:internal/process/task_queues:96:5) { errors: [ { message: 'Login Required.', domain: 'global', reason: 'required', location: 'Authorization', locationType: 'header' } ] }
CodePudding user response:
Lets look at the working example you have
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: range
Notice how you have added auth and set it to your jwtClient? This adds the authorization permission to your call.
Now lets look at your update statment
sheets.spreadsheets.values.update({
spreadsheetId,
range:"Sheet1!C3",
valueInputOption,
requestBody,
You have not added auth, which means you are trying to preform this action without being authenticated. Update acts upon a users private data you need permission to be able to call this method.
So by that logic the following should fix your error
sheets.spreadsheets.values.update({
auth: jwtClient,
spreadsheetId,
range:"Sheet1!C3",
valueInputOption,
requestBody,