0

I am trying to access Google's DialogFlow API using nodeJS and get the following error:

Error: Could not load the default credentials.

It finds the API key fine and loads the client, but wont run the query under localhost. It seems to be looking for the default credentials, but I am specifying ones from the service account.

    const {SessionsClient} = require('@google-cloud/dialogflow').v2;
    const dfKey=JSON.parse(fs.readFileSync("./assets/keys/dialogflow.json"));
    const dfClient=new dialogflow.SessionsClient({ private_key:dfKey.private_key,client_email:dfKey.client_email });

    const detectIntent = async (queryText, sessionId="123456789") => {
        let sessionPath=dfClient.projectAgentSessionPath(dfKey.project_id, sessionId);
        let request = { session: sessionPath, queryInput:{ text: {  text: queryText, languageCode: "en" } } };
        const responses = await dfClient.detectIntent(request);
        console.log(responses);
        const result = responses[0].queryResult;
        console.log(result);
        return { response: result.fulfillmentText };
    }
    
detectIntent("How are you")

It finds the API key fine and loads the client, but wont run the query under localhost. It seems to be looking for the default credentials, but I am specifying ones from the service account.

Bill Ferster
  • 347
  • 1
  • 5
  • 17

2 Answers2

0

I think it is not loading the default credential using the GCP API I set in my .env something like

GOOGLE_APPLICATION_CREDENTIALS=./google_key/creds.json

Hope it helps

Ok looking at an other solution

  1. method GOOGLE_APPLICATION_CREDETIALS
  const sessionClient = new dialogflow.SessionsClient({
      projectId,
      keyFilename: credentials_file_path,
    });

this is the reference How authenticate with gcloud credentials an Dialogflow API

Balint
  • 295
  • 2
  • 11
0

I'm not very familiar with node, but in the few prototypes I dealt with I needed to explicitly declare the auth keys instead of simply pointing the client instantiation to the key.

Take a look at this example. maybe it could be a good boilerplate code to build yours upon.

Just out of curiosity - Have you double checked if the service account you are using has the proper dialogflow roles assigned (ie. Dialogflow API Client)?

Luciano Martins
  • 421
  • 1
  • 9