googleapis / google-cloud-node

Google Cloud Client Library for Node.js
https://cloud.google.com/nodejs
Apache License 2.0
2.86k stars 582 forks source link

NodeJS Google Cloud Speech To Text - LongRunningRecognize - Could not load the default credentials #4897

Open royibernthal opened 6 months ago

royibernthal commented 6 months ago

Environment details

Steps to reproduce

I'm using Google Cloud Speech To Text in NodeJS.

I specified the correct path to the google cloud credentials in process.env.GOOGLE_APPLICATION_CREDENTIALS.

The normal recognize works as expected:

const [response] = await speechClient.recognize(options);

However, the long running recognize throws an error:

const [operation] = await speechClient.longRunningRecognize(options);
const [response] = await operation.promise();

The second line throws the following error:

Could not load the default credentials.

localhostd3veloper commented 6 months ago

@royibernthal Well actually,

GOOGLE_APPLICATION_CREDENTIALS should be a path of your private credentials key in JSON format

and that should be stored in the GLOBAL ENVIRONMENT of the system.

royibernthal commented 6 months ago

That's what exactly what I did :)

localhostd3veloper commented 6 months ago

Did you add the variable like this? https://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html

and restarted the server?

And also if you have done that, there is no need to import it using process.env it will automatically be imported

royibernthal commented 6 months ago

I'm using dotenv locally and injecting env vars to the container on aws in production. At the end of the day process.env holds the env vars of the running nodejs process, does it really matter how they got there? Even setting them manually should do the trick no?

royibernthal commented 6 months ago

Notice that the normal recognize does use the credentials in the env vars properly (path that leads to the file that contains the credentials), and only the second line in long recognize doesn't.

sofisl commented 6 months ago

Hm, could you try running: gcloud auth application-default login? Still weird that one authentication method works for one and not the other.

idodekerobo commented 4 months ago

loosely related, but searching online and not seeing any ways to initialize Google Cloud without keyFileName - which isn't ideal for production cloud environments. JSON file is fine for local development, but what is the expectation for hosting service account in the cloud?

and even though in the documentation it says you can initialize via javascript object, i'm getting errors when trying to pass credentials directly in via environment variables like below. it seems the Cloud Storage environment automatically checks for process.env.GOOGLE_APPLICATION_CREDENTIALS which doesn't exist when passing in via js object. unsure if thats a bug or if passing in via js object is not the right approach.

any feedback would be appreciated!

const gCloudStorage = new Storage({
    projectId: `project id`,
    credentials: {
        // credentials loaded via .env variables
    }
 })
idodekerobo commented 4 months ago

i've also tried to pass the json directly into my .env variable, like below, and then parsing the JSON like this in my app. but that gives me a ENAMETOOLONG error. i guess it thinks the json body is the file name???

.env file GOOGLE_APPLICATION_CREDENTIALS=service-acct-json-body

when accessing storage

const gCloudStorage = new Storage({
   projectId: 'project-id',
   credentials: JSON.parse(
      process.env.GOOGLE_APPLICATION_CREDENTIALS.replace(/\n/g,"")
   )
})