Error while trying to call a cloud function using service account

67 views Asked by At
const { GoogleAuth } = require('google-auth-library')
const auth = new GoogleAuth();
    async function func1() {
      const client = await auth.getIdTokenClient(targetAudience);
      const requestOptions = {
        url: targetAudience,
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        data: data
      }
      console.log("calling target audience")
      const res = await client.request(requestOptions);
      return res
    }


    console.log("calling func1")
    func1()
      .then(function (response) {
        return resolve({res: 'done'})
      })
      .catch(function (error) {
        return resolve({res: 'fail' })
      })

I am trying to call an internal CF which is the target audience in this case.

I am getting the below error: Unsuccessful response status code. Request failed with status code 404

"calling func1" is getting printed but "calling target audience" is not getting printed. Looks like it is failing before coming to this line:

const res = await client.request(requestOptions);

I am not sure why it is failing at this. I have checked the permissions given to the gcp service account and I dont see anything amiss. These are the permissions I have given:

Cloud Functions Admin
Service Account OpenID Connect Identity Token Creator
Service Account Token Creator
Storage Admin 

Could anyone help me out here?

If I could identity the cause I might be able to proceed with finding a solution but I am not understanding why this error is coming. Target audience is correct and have observed that even if we give some string I saw that it would reach line:

const res = await client.request(requestOptions);

Currently even that is not happening. Is there some permission that I am missing or where am I going wrong?

1

There are 1 answers

1
Theo Zhang On

Try to see if "targetAudience" exist. Also Double-check that the targetAudience is in the correct URL format for the Cloud Function. It should be in the format: https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME.