'How do I set up a Google Cloud Function with Authentication?

Disclaimer: I'm completely new to Google Cloud Functions and serverless functions in general.

I've set up a basic Google Cloud Function, set it to HTTPS trigger and Require Authentication, and given the service account and my account the cloud function invoker role.

However, whenever I use Postman to sent an HTTP request to the function using my oauth2 token, I get a 401 Unauthorized error.

I've also followed the steps here: https://cloud.google.com/functions/docs/securing/authenticating

to create and download the service account key and make an HTTP request using cURL and get the same error.

How do I need to set this up so that authorized users can send HTTP requests to this function?

Here's what I'm doing:

$gcloud auth login (to log in with my account with cloud invoker permission)
$curl https://my-trigger-url \ -H "Authorization: bearer $(gcloud auth print-identity-token)"


Solution 1:[1]

Guillaume found the solution:

My cURL syntax to trigger the Cloud Function needed the -H "Authorization: bearer $(gcloud auth print-identity-token)" before the URL instead of after, which is the opposite of Google's own documentation.

Thanks for your help.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Moses Nester