Error: 16 UNAUTHENTICATED: Failed to retrieve auth metadata with error: Could not refresh access token

Viewed 6951

The complete error is as follows:

Unhandled error Error: 16 UNAUTHENTICATED: Failed to retrieve auth metadata with error: Could not refresh access token: Unsuccessful response status code. Request failed with status code 500
at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:327:49)
at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:299:181)
at /workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:145:78
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Caused by: Error
at Query._get (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:1449:23)
at Query.get (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:1438:21)
at Object.getTeacherDataWithFilters (/workspace/lib/teachers/methods.js:168:81)
at /workspace/lib/teachers/callable.js:22:36
at func (/workspace/node_modules/firebase-functions/lib/providers/https.js:273:32)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
code: 16,
details: 'Failed to retrieve auth metadata with error: Could not refresh access token: Unsuccessful response status code. Request failed with status code 500',
metadata: Metadata { internalRepr: Map {}, options: {} }

I recently deployed to the firebase functions and now I am getting this error. I dont know what has happened and I could not find anything specific to this

5 Answers

I faced the same error, it was a authentication issue due to credentials. Below command fixed it for me.

 gcloud auth application-default login

I got below output after running this command.

Credentials saved to file: [/Users/username/.config/gcloud/application_default_credentials.json]. These credentials will be used by any library that requests Application Default Credentials (ADC).

Official Document for Reference

I have figured out the actual reason and that's why I am answering my own question. The reason why I was getting this error is that the default service account attached to this project which is mostly like <project-ID>@appspot.gserviceaccount.com was disabled. After enabling, everything worked fine.

In case someone deploying on Vercel, there's a minor change you need to do when setting PRIVATE_KEY env variable. You need to remove starting and ending double quote from key. Save it like this:

-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgk
.
.
.
4xxjFxhpK5GLYmGSZzpv5Y=\n-----END PRIVATE KEY-----\n
  1. Try saving .env file in UTF-8
  2. What worked for me: process.env.TOKEN.replace(/\n/gm, '\n')

In case it's working fine in your local environment but somehow failing in any production build, here's a nice hack to get it sorted in any platform:

Also, this works like a charm in Vercel (Note that while adding environment variables in Vercel you must not put any quotations and paste it directly like: {"privateKey":"XXX...."}

//.env.local File:
FB_ADMIN_PRIVATE_KEY='{"privateKey":"XXX...."}'

And your configuration:

import admin from 'firebase-admin';

const keyString = process.env.FB_ADMIN_PRIVATE_KEY ?? '{"privateKey": ""}'; //Since it's a TypeScript so need to ensure that I pass a valid string to the JSON.parse as FB_ADMIN_PRIVATE_KEY can be undefined

const { privateKey } = JSON.parse(keyString);

if (privateKey === '') {
  //In case you want to confirm things in Server Logs
  console.log('FIREBASE_PRIVATE_KEY is not set');
}

admin.initializeApp({
  credential: admin.credential.cert({
    clientEmail: process.env.FB_ADMIN_CLIENT_EMAIL,
    privateKey: privateKey,
    projectId: process.env.FB_ADMIN_PROJECT_ID,
  }),
});

const db = admin.firestore();
const auth = admin.auth();

export { auth, db };
Related