I wanted to post what solved my issue. The root cause was because my Cloud Function call had an extended route and parameters associated with it, which was causing issues when getting the client with getIdTokenClient
.
I was originally calling getIdTokenClient('https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME/ROUTE?PARAMETER=ARGUMENT')
with the extended route and parameters included. Instead, I should not have been including the extended route and parameters, only the actual function URL. So it would look something like this instead getIdTokenClient('https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME')
.
I'm not sure why it was working locally for me, maybe some other authentication settings were conflicting with the code.
I've provided some updated code below that is working when deployed to Cloud Run. A similar update would work for the code that @guillaumeblaquiere provided.
import { GoogleAuth } from 'google-auth-library';
async function callCloudFunction() {
const functionUrl = 'https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME';
const functionRoute = '/ROUTE?PARAMETER=ARGUMENT';
const auth = new GoogleAuth();
const client = await auth.getIdTokenClient(functionUrl);
const token = await client.getRequestHeaders();
const res = await fetch(functionUrl + functionRoute, {
method: 'GET',
headers: token
});
const data = await res.json();
return data
}
I appreciate the responses from everyone who provided potential solutions.