How I fixed the Google Spreadsheet JWT 403 PERMISSION_DENIED error
On an Autowini project, I got a PERMISSION_DENIED error while fetching multilingual data from a Google spreadsheet with Node.js.
{
"code": 403,
"message": "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
"status": "PERMISSION_DENIED"
}🔍 Tracking down the cause
At first, I tried to fix it through the spreadsheet's sharing settings and by reissuing the IAM service account key.
- Gave the service account email editor access in the Google spreadsheet
- Reissued the service account key in Google Cloud Console IAM
But with only these steps, the error kept happening.
❗️ The cause
In version ^4.1.4 of the google-spreadsheet package, some calls with JWT auth were treated as an unauthorized caller (403 Error).
In other words, no matter how much I changed the IAM or spreadsheet permission settings, the error came from the package version itself not fully supporting that way of handling auth.
✅ The fix
I fixed the problem by upgrading the google-spreadsheet package to ^5.0.2.
# pnpm
pnpm add google-spreadsheet@^5.0.2After the upgrade, accessing the spreadsheet with JWT auth loaded the data correctly.
import { GoogleSpreadsheet } from 'google-spreadsheet';
import { JWT } from 'google-auth-library';
import { credentials } from './credentials.js';
const doc = new GoogleSpreadsheet(SPREADSHEET_DOC_ID);
await doc.useServiceAccountAuth(new JWT({
email: credentials.client_email,
key: credentials.private_key,
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
}));
await doc.loadInfo();💡 What I took away
- I felt that when you hit the same error, checking for a package version problem first can be a quick way to fix it.
- Someone on GitHub had actually run into the same problem (issue #723), and I left a comment on that issue too.
- #403
- #Google Spreadsheet JWT
- #PERMISSION_DENIED