Reputation: 4250
I have a node application and I'm trying to use the google language api. I want to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the json file in the same directory (sibling to package.json and app.js).
I had tried process.env.GOOGLE_APPLICATION_CREDENTIALS = "./key.json";
in my app.js file (using express), but it isn't working. I have also tried putting "GOOGLE_APPLICATION_CREDENTIALS":"./key.json"
in my package.json and that didn't work as well. It DOES work when I run in the terminal export GOOGLE_APPLICATION_CREDENTIALS="./key"
.
Here is the error message:
ERROR: Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
Any tips are appreciated, thanks!
Upvotes: 9
Views: 19289
Reputation: 1349
If you are using npm package of google-auth-library, then this will be much easier.
To make things easier download the service account credentials json file to get same permission as some service account you are trying to access and store it in a safe place in your local.
Run the below code to access the secured endpoint or service using the google-auth-library.
const {GoogleAuth} = require('google-auth-library');
this.client = null;
this.GCP_SECURED_ENDPOINT = 'https:www.secured.gcp.com';
const getAuthClient = async () => {
if (!this.client) {
const auth = new GoogleAuth({
projectId: '<ProjectID>',
keyFile:
'<Path to service account credentials json file >',
});
// const project = await auth.getProjectId();
this.client = await auth.getIdTokenClient(this.GCP_SECURED_ENDPOINT);
}
return this.client;
};
const publish = async crawlTask => {
const client = await getAuthClient();
const {data} = await client.request({
url: `${this.GCP_SECURED_ENDPOINT}`,
method: 'POST',
data: crawlTask,
});
return data;
};
const requestData = {};
publish(requestData);
Upvotes: 0
Reputation: 23
Just to update this thread. Relative paths with GOOGLE_APPLICATION_CREDENTIALS now works fine with dotenv :)
Just store your service-account credentials as a file in your project and reference it relative to your env-file as a path. Then it should work fine :)
Upvotes: 1
Reputation: 328
After reading and reading about this issue on the internet, the only way to resolve this issue for me was to declare the environment variable for the node execution:
GOOGLE_APPLICATION_CREDENTIALS="./key.json" node index.js
Because I was able to print the token from my server console, but when I was running the node application, the library was unable to retrieve the system environment value, but setting the variable for the execution, it was able to retrieve the value.
Upvotes: 10
Reputation: 6032
The issue is covered mostly in the docs here.
the command varies slightly depending on what your OS is:
Upvotes: 0
Reputation: 2099
It could be that the environment variable in your OS was not accurately set. For example in Linux you usually have to set GOOGLE_APPLICATION_CREDENTIALS in the terminal where you executed your app.
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Another option you have is passing the json path by code. It is documented this process using Node.js with the Cloud Storage.
Upvotes: 1
Reputation: 90
I encountered the same issue. I was able to get it working by doing the following:
export GOOGLE_APPLICATION_CREDENTIALS="//Users/username/projects/projectname/the.json"
Upvotes: 0
Reputation: 342
The GOOGLE_APPLICATION_DEFAULT environment variable you are setting is accessed by the google client libraries - it wont work with a relative path, you'll need to set the absolute path.
Upvotes: 0