Using a service account file to access google sheet is working as expected
import { google } from 'googleapis';
import serviceAccount from 'serviceKey.json';
const spreadsheetId = 'spreadsheet-id';
const sheets = google.sheets({ version: 'v4' });
const jwtClient = new google.auth.JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const appendValue = async () => {
await jwtClient.authorize();
await sheets.spreadsheets.values.append({
auth: jwtClient,
spreadsheetId,
range: 'Test!A2:E2',
valueInputOption: 'RAW',
requestBody: { values: [['A', 'B', 'C', 'D', 'E']] },
});
};
But trying it using default application credentials throws an error saying Login is Required.
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/serviceKey.json"
import { google } from 'googleapis';
const spreadsheetId = 'spreadsheet-id';
const sheets = google.sheets({ version: 'v4' });
const appendValue = async () => {
await sheets.spreadsheets.values.append({
spreadsheetId,
range: 'Test!A2:E2',
valueInputOption: 'RAW',
requestBody: { values: [['A', 'B', 'C', 'D', 'E']] },
});
};