Summary:
I have deployed a very basic React app packaged as a Cloud Run app. I want to call another Cloud Run app (API) via a service account assigned to the service. I have no issue with this in my Express Node Cloud Run apps. Using the same google-auth-lib and example as below. What am I doing wrong with this React App that dont seem to follow through with calling the other API? (Locally with the SA key.json set it works)
My code:
React class beeing rendered:
const {GoogleAuth} = require('google-auth-library');
class Overview extends Component {
async componentDidMount(){
console.log("Trying to auth to google");
const url = "https://myapp.run.app/";
const targetAudience = "12345.apps.googleusercontent.com";
// only for locally accessing the service account
const options = {
credentials: require('../key-credentials.json'),
projectId: 'myprojectid'
};
// In the deployed app the "option" parameter is not included
const auth = new GoogleAuth(options);
const client = await auth.getIdTokenClient(targetAudience);
const res = await client.request({url});
console.info(res.data);
}
render(){
return (
<div>
<h1>Overview</h1>
</div>
)
}
}
export default Overview;
Link to the docs with the google-auth-lib example: https://cloud.google.com/run/docs/authenticating/service-to-service#run-service-to-service-example-nodejs
The Error:
Screenshot over the browser console
GET https://metadata.google.internal./computeMetadata/v1/instance net::ERR_NAME_NOT_RESOLVED
This error I understand as the Cloud Run service trying to find the google cloud meta data which holds the service account that has access to the url trying to be called, but the meta data cant be found.
TypeError: Unexpected error determining execution environment
This I beleve is that because the service account meta data cant be found, the environment can be correct to call the requested service.
Any idea what could be wrong?