Experiencing Google Sheets API authorization issues with Node.js
I’m currently working on a project involving the Google Sheets API using Node.js. Initially, everything was functioning well as I closely followed the official quickstart guide. The authentication process was successful, and I could access my spreadsheet data smoothly.
However, I started encountering errors related to authentication, specifically stating The request does not have valid authentication credentials
. In an attempt to fix this, I tried several approaches, such as adjusting the scope permissions and altering various settings, but none were effective.
As a last resort, I chose to reset everything. I deleted my entire Google Cloud project, wiped all credentials, and set everything up from scratch. Now, whenever I create a new project folder, write the quickstart code, install necessary packages, and execute it, I run into the following error:
const { google } = require('googleapis');
const fs = require('fs');
async function authenticateAndRead() {
try {
const auth = new google.auth.GoogleAuth({
keyFile: 'service-account.json',
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly']
});
const client = await auth.getClient();
const sheets = google.sheets({ version: 'v4', auth: client });
// This line causes the error: unauthorized_client
const response = await sheets.spreadsheets.values.get({
spreadsheetId: 'my-sheet-id',
range: 'Sheet1!A1:C10'
});
} catch (error) {
console.log('API returned an error:', error);
}
}
I have recreated the service account credentials several times and rewritten the quickstart code from the ground up. What could be the reason for this unauthorized_client error in a completely fresh setup?