1
votes

I have a Firebase Function that subscribes to a Cloud PubSub topic. App is initialized very simply like this:

import * as admin from 'firebase-admin';

admin.initializeApp();

I'm getting this error:

"Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information. at GoogleAuth.getApplicationDefaultAsync (/srv/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19) at process._tickCallback (internal/process/next_tick.js:68:7)"

Here's the weird thing. It typically works. In other words, if I trigger it a second time it works. And a third time. Most often it seems to fail the first time it runs after a new firebase deploy and possibly on a "cold start."

Not sure what I'm doing wrong and why it would fail only on the first run.

1
If you do a web search on this error message, you'll find it comes up a lot. I suggest doing some research to figure out what sorts of things work for different people. Be sure your modules are all up to date. Try both node 8 and node 10 runtimes.Doug Stevenson
Most of what I have seen says to add the service account json file. Admittedly, I haven't done that yet. However, it doesn't seem to be necessary. I don't use it for anything else AND literally the SAME exact thing that fails the first time I execute it will pass a minute later when I try again. And continue to be fine... until I deploy a new update. Then it fails the first time again. I don't understand the inconsistency.Jason Byrne
You shouldn't have to add other service accounts - the default service account should be available. The problem might be in the particular version of a library in use, or the choice of runtime. If you cannot narrow it down, contact Firebase support for assistance. support.google.com/firebase/contact/supportDoug Stevenson
I thought I had solved it, but it just turns out to be somewhat intermittent. However, always works after the first attempt (with same request). I believe this has to do with a "cold start" because I saw something in the log earlier tonight that had it after a couple hours of inactivity. Seems to be related to this: github.com/googleapis/google-auth-library-nodejs/issues/798Jason Byrne

1 Answers

3
votes

SOLVED! This answer helped: Error: Could not load the default credentials (Firebase function to firestore)

From within a Firebase Function for an API call, I was publishing to a Cloud PubSub topic like this:

pubsub.topic(topicName).publish(dataBuffer, customAttributes)

I was not awaiting the response and was immediately sending the 2XX HTTP response back to the client. The execution seemed to continue fine, but obviously it did not behave as intended.

Sometimes the API response call itself would fail (and never publish the message), but sometimes not. In other cases, the publish would succeed but the Firebase Function subscribing to the topic would fail!

In all cases, this seemed to resolve itself after running the script a second time. For this reason, I still believe it had something to do with a cold start.

But since I changed it to await like this:

await pubsub.topic(topicName).publish(dataBuffer, customAttributes)

I have not seen this problem happen again.