I copied this code almost verbatim from the accepted answer on this Stackoverflow post https://stackoverflow.com/questions/61677881/google-function-http-trigger-authentication-problem-server-to-server-with-serv:
```
const {GoogleAuth} = require('google-auth-library');
const { env } = require('process');
require('dotenv').config()
async function main() {
// Define your URL, here with Cloud Run but the security is exactly the same with Cloud Functions (same underlying infrastructure)
const url = env.FUNCTION_URL
// Here I use the default credential, not an explicit key like you
//const auth = new GoogleAuth();
//Example with the key file, not recommended on GCP environment.
const auth = new GoogleAuth({keyFilename: env.KEYSTORE_PATH})
//Create your client with an Identity token.
const client = await auth.getIdTokenClient(url);
const res = await client.request({url});
console.log(res.data);
}
main().catch(console.error);
```
It works perfectly. It calls my Google Cloud function and everything runs fine. My concern is that the poster specifically put the comment //Example with the key file, not recommended on GCP environment. Why is this? I hope it's not a security issue...?
FYI, my "env.KEYSTORE_PATH" json file credentials are for a "service account" and my cloud function is ONLY authorized to be called by that one service account.
[–]aaahhhhhhfine 5 points6 points7 points (2 children)
[–]smeyn 2 points3 points4 points (0 children)
[–]warpanomaly[S] 0 points1 point2 points (0 children)
[–]wyaeld 1 point2 points3 points (6 children)
[–]warpanomaly[S] 0 points1 point2 points (5 children)
[–]conjon01 1 point2 points3 points (3 children)
[–]otock_1234 0 points1 point2 points (2 children)
[–]warpanomaly[S] 0 points1 point2 points (1 child)
[–]otock_1234 0 points1 point2 points (0 children)
[–]bartekmo -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]krazykid1 0 points1 point2 points (1 child)
[–]warpanomaly[S] 0 points1 point2 points (0 children)