PLEASE help us help you by writing a good post!
My current deployment doesn’t have an environment variable GOOGLE_APPLICATION_CREDENTIALS which points to a JSON file that contains the Service Account Key.
I’m needing assistance on how to setup this environment variable such that it can access the file in the deployed version of the site.
I have had no issues locally due to the gcloud CLI using a application_default_credentials.json in it’s config, but I am at a loss on how to include this in a production build such that it can be called by a google client application library.
As for my use-case, I’m looking to use reCAPTCHA v3, and it requires authentication to use their client library, is this at all possible?
1 Like
Are you comitting the file to your repo? If yes, you can refer to: How to Include Files in Netlify Serverless Functions
If not, you’d have to add each value seperately as an environment variable and generate the JSON file on the fly. You can then write that file to the /tmp
dir and point the library to that.
Hello, in case you haven’t found a way to do it yet, I think this might help you. I didn’t create a temporary file; instead, I took the variables and sent them directly to the function.
const credentials = {
// eslint-disable-next-line no-undef
type: process.env.TYPE,
// eslint-disable-next-line no-undef
project_id: process.env.PROJECT_ID,
// eslint-disable-next-line no-undef
private_key_id: process.env.PRIVATE_KEY_ID,
// eslint-disable-next-line no-undef
private_key: process.env.PRIVATE_KEY.replace(/\n/gm, ‘\n’),
// eslint-disable-next-line no-undef
client_email: process.env.CLIENT_EMAIL,
// eslint-disable-next-line no-undef
client_id: process.env.CLIENT_ID,
// eslint-disable-next-line no-undef
auth_uri: process.env.AUTH_URI,
// eslint-disable-next-line no-undef
token_uri: process.env.TOKEN_URI,
// eslint-disable-next-line no-undef
auth_provider_x509_cert_url: process.env.AUTH_PROVIDER_X509_CERT_URL,
// eslint-disable-next-line no-undef
client_x509_cert_url: process.env.CLIENT_X509_CERT_URL,
};
try {
const auth = new GoogleAuth({
credentials: credentials,
scopes: [‘https://www.googleapis.com/auth/cloud-platform’],
});
const client = new RecaptchaEnterpriseServiceClient({ auth });
const projectPath = client.projectPath(projectID);
const request = {
assessment: {
event: {
token: token,
siteKey: siteKey,
},
},
parent: projectPath,
};