3

I noticed error in my Cloud Function invocation via Pub/Sub. Every time it throws an 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)

I already redeployed it with specific credentials for use. And refactored my code accordingly to that proposal https://stackoverflow.com/questions/58127896/error-could-not-load-the-default-credentials-firebase-function-to-firestore/58779000#58779000

But nothing has been changed. It looks like Google released some updates and it was crashed. I think so because of our mobile app, wich does use Firebase SDK and invokes cloud functions directly. But it's wrote on Swift but my Cloud Functions is wrote on JS. So it means that we are using DIFFERENT libs, but have THE SAME ISSUE. Does somebody know something about that issue ? It would be super helpful for my team. Thanks in advance.

Denys Kobzar
  • 31
  • 1
  • 2
  • 1
    *UPDATE*: It have been started since we changed NodeJS version for Cloud Functions from 8 to 10 (Which currently is beta on GCP). So, if you noticed the same issue in your project - please check NodeJS version and try to redeploy your functions within 8th version of NodeJS. Hope it'll be helpful for you. – Denys Kobzar Jan 08 '20 at 15:27
  • I came across [this GitHub Issue](https://github.com/googleapis/google-auth-library-nodejs/issues/964) which could be helpful. – Artemis Georgakopoulou Aug 04 '20 at 12:16
  • At the moment there is NodeJS 8 is depreciated at GCP, but you can use 10,12,14 do you think this issue will still appear? – vitooh Feb 01 '21 at 12:16

2 Answers2

1

As mentioned in the github issue

creating a client in the global scope outside of your function will solve this issue see: function execution lifetime.

If your code looks like this:

const {CloudTasksClient} = require('@google-cloud/task');
// CloudTasksClient, could be Storage, or Vision, or Speech.
const task = new CloudTasksClient();
export.helloWorld = (req, res) => {
  res.send('hello world');
}

Instead write:

const {CloudTasksClient} = require('@google-cloud/task');
// CloudTasksClient, could be Storage, or Vision, or Speech.
let task;
export.helloWorld = async (req, res) => {
  if (!task) {
    task = new CloudTasksClient();
  }
  await task.doSomething(); // or storage.doSomething(), etc.
  res.send('hello world');
}

There is work in motion to shield folks from this issue. But, as described in the execution timeline, the core of the problem is that asynchronous work cannot happen outside of the request cycle of your function.

This may also fix the problem

You can set an environment variable on a cloud function when deployed using the firebase-tools CL

  • Set the env variable DEBUG_AUTH to true

  • Run

    npm install --save @google-cloud/firestore@latest gcp-metadata@latest

There are two ways to do this:

1.The official (old) way is to set the variable using gcloud or pantheon. The CLI will respect the variable on future deploys.

2.You can opt into the not-yet-public support for managing env variables with dot-env files. This can be done by calling:

firebase --open-sesame dotenv
#From their functions directory    
echo "DEBUG_AUTH=true" > .env  
firebase deploy --only functions
0

This issue occurs due to many reasons and the solution may vary based on that,

You can try using gcloud auth application-default login as you might not be logged in because of which you may end up receiving this error, you can see this stackoverflow thread for more information on it.

You can explicitly specify what service account to use instead of the default as documented under authentication.

You can also try to set the environment variable DETECT_GCP_RETRIES to 3 and see if it works. This can be done on deployment of the function, via set or update in gcloud.

Finally, As you said, it might be because of the version you have used. So try changing it to a stable version.

Pit
  • 184
  • 11
Zeenath S N
  • 153
  • 5