Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Functions: Error: Unexpected error while acquiring application default credentials: read ECONNRESET

When trying to run my firebase cloud function with http trigger, I kept getting this error but only intermittently:

Error: Unexpected error while acquiring application default credentials: read ECONNRESET 

There are some unrelated ECONNRESET/firebase SO answers that provided no solution: Firebase Storage & Cloud Functions - ECONNRESET

like image 436
Joshua Dyck Avatar asked Jan 12 '18 20:01

Joshua Dyck


1 Answers

Problem:

To initialize my functions I was using:

const functions = require('firebase-functions');
const admin = require('firebase-admin');    

admin.initializeApp(functions.config().firebase);

Solution (for me at least):

I had to use my Google service account credentials:

var serviceAccount = require("./PATH_TO_YOUR_SERVICE_ACCOUNT_FILE_GOES_HERE.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://YOUR_FIREBASE_PROJECT_NAME.firebaseio.com"
});

To get the firebase project name check the top left corner of your firebase project console (A) .

To Download the service account JSON file click on the (B) gear icon in the top left of the firebase console then click (C) project settings:

enter image description here

Then click the service accounts tab:

enter image description here

Then click the "Generate New Private Key to download the file.

You can then move the JSON file into your project functions folder and import it as shown in the code snippet above.

enter image description here

like image 162
Joshua Dyck Avatar answered Oct 12 '22 11:10

Joshua Dyck