Node.js Google Sheets API displays unauthorized_client error on new setup

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?

Had this exact problem a few months ago - it’s a timing issue with service account activation. Google Cloud Console shows the account as created, but there’s usually a 5-10 minute delay before it actually works across all Google services. That unauthorized_client error means the Sheets API doesn’t recognize your service account yet. Wait about 10 minutes after creating the service account, then try your code again. Also make sure you’ve enabled the Google Sheets API in your project’s API library - not just created the service account. Easy to forget that step when you’re setting everything up.

Check if your service account has access to the specific spreadsheet you’re trying to read. Even with proper credentials and API enabled, the service account needs explicit permission to access your sheet. Go to your Google Sheet, click Share, and add your service account email (it’s in the service-account.json file under client_email) with at least Viewer permissions. People miss this all the time when recreating everything from scratch. The unauthorized_client error usually means the service account exists but can’t access the resource - your credentials are fine.

Check your service account json file path - I know it sounds obvious but I’ve seen this trip ppl up when they rebuild everything. Make sure you downloaded the right key and it’s not corrupted. Try dropping the json file in the same folder as your script, and just use the filename instead of the full path. Google Cloud Console sometimes glitches when generating keys on brand new projects.