Google Cloud scheduler call to Cloud functions 401 error

I used to run some functions developed and deployed on Google Cloud Run Functions.

Some of them are scheduled to run via Cloud Scheduler.

Last time I decided to copy the definition of the Cloud Scheduler job from bot 2 to my new bot3 function. And I started to get 401 unauthenticated errors for bot3, but the same definition was working fine for bot2.

The error looks like below:

debugInfo: "URL_ERROR-ERROR_AUTHENTICATION. Original HTTP response code number = 401"
jobName: "projects/....../locations/europe-west6/jobs/......"
status: "UNAUTHENTICATED"
targetType: "HTTP"

and in the Cloud Function log is the misleading:

The request was not authorized to invoke this service. Read more at https://cloud.google.com/run/docs/securing/authenticating Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#401

BUT – actually the problem is that during the copy of the scheduler job definition, the “Audience” value IS NOT updated with the new function URL (in my case bot3), but the old function URL is kept. It seems like it was autofilled in bot2 definition though and I didn’t noticed it. So be sure that URL and Audience are properly set!

If you fix this the 401 error is solved and gone!!!

I hope I saved you some time. Post one thanks in comments if so …