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
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:
Then click the service accounts tab:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With