google-cloud-node icon indicating copy to clipboard operation
google-cloud-node copied to clipboard

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

Open royibernthal opened this issue 2 years ago • 8 comments

Environment details

  • which product (packages/*): @google-cloud/speech
  • OS: Windows 10
  • Node.js version: 18.15.0
  • npm version: 9.3.1
  • google-cloud-node version: 6.1.0 (@google-cloud/speech)

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.

royibernthal avatar Dec 18 '23 17:12 royibernthal

@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.

localhostd3veloper avatar Dec 30 '23 07:12 localhostd3veloper

That's what exactly what I did :)

royibernthal avatar Dec 30 '23 08:12 royibernthal

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

localhostd3veloper avatar Dec 30 '23 08:12 localhostd3veloper

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 avatar Dec 31 '23 03:12 royibernthal

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.

royibernthal avatar Dec 31 '23 03:12 royibernthal

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

sofisl avatar Jan 03 '24 00:01 sofisl

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 avatar Feb 22 '24 11:02 idodekerobo

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,"")
   )
})

idodekerobo avatar Feb 22 '24 12:02 idodekerobo