1

I have created a Dialogflow agent with a concurrent gcloud app. However, when I try to integrate it into a Node.js backend, it seems to be accessing the wrong application default credentials.

I can confirm that it is verifying authentication is successful because it console logs the correct project ID (diagnosistest-56e81) when I run this code:

// Imports the Google Cloud client library.
const Storage = require('@google-cloud/storage')

// Instantiates a client. If you don't specify credentials when constructing
// the client, the client library will look for credentials in the
// environment.
const storage = new Storage();

// Makes an authenticated API request.
storage
  .getBuckets()
  .then((results) => {
    const buckets = results[0];

    console.log('Buckets:');
    buckets.forEach((bucket) => {
      console.log(bucket.name);
    });
  })
  .catch((err) => {
    console.error('ERROR:', err);
  })

However, when I try to connect dialogflow, I get an error.

Code to connect to dialogflow:

// You can find your project ID in your Dialogflow agent settings
const projectId = 'diagnosistest-56e81'; //https://dialogflow.com/docs/agents#settings
const sessionId = 'quickstart-session-id';
const query = 'hello';
const languageCode = 'en-US';

// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();

// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);

// The text query request.
const request = {
  session: sessionPath,
  queryInput: {
    text: {
      text: query,
      languageCode: languageCode,
    },
  },
};

// Send request and log result
sessionClient
  .detectIntent(request)
  .then(responses => {
    console.log('Detected intent');
    const result = responses[0].queryResult;
    console.log(`  Query: ${result.queryText}`);
    console.log(`  Response: ${result.fulfillmentText}`);
    if (result.intent) {
      console.log(`  Intent: ${result.intent.displayName}`);
    } else {
      console.log(`  No intent matched.`);
    }
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

Error message that I receive:

ERROR: { Error: 7 PERMISSION_DENIED: Dialogflow API has not been used in project 764086051850 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=764086051850 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

The weird thing is, my project number is 889800549397 and not 764086051850. So it seems like it's not accessing the correct credentials.

What I've tried so far:

  1. Create new Dialogflow service account key and setting export to the json key file via export GOOGLE_APPLICATION_CREDENTIALS='/Users/Joseph/workspace/finddoc/DiagnosisTest-cred.json
  2. Deleting application_default_credentials.json file in ./config/gcloud/ folder.

When I do this I get a different 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.
Zachary Newman
  • 15,448
  • 2
  • 36
  • 36
doctopus
  • 4,073
  • 4
  • 30
  • 65

2 Answers2

2

Finally figured out what the problem was. After setting the environment variable GOOGLE_APPLICATION_CREDENTIALS, you have to restart your computer so it can take effect!

doctopus
  • 4,073
  • 4
  • 30
  • 65
  • Where have you exported GOOGLE_APPLICATION_CREDENTIALS? I have followed this [doc](https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-python) and could access using json key without restart (I did it via Gcloud Shell) – enle lin May 30 '18 at 12:46
  • Actually i included it in my .bash_profile like so. `export GOOGLE_APPLICATION_CREDENTIALS = [path]`. It seemed like if I just typed the command in terminal then it wouldn't set it as an environment variable permanently. I also followed the same tutorial as you – doctopus May 30 '18 at 14:10
  • OK. Agree that the environment variable won't be permanent if executed in terminal. I understood that it is not working for you when run the command in the terminal. Just to add some notes, I think it is not necessary to restart the machine, you can reload the .bash_profile file with EXEC command as explained in [this question](https://stackoverflow.com/questions/2518127/how-do-i-reload-bashrc-without-logging-out-and-back-in/9584378#9584378) – enle lin May 30 '18 at 14:37
0

I was experiencing this exact same error message (including 764086051850 project id) on my local machine when trying to use the Dialogflow V2 API. I didn't want to bother with the GOOGLE_APPLICATION_CREDENTIALS environment variable so I followed the instructions here.

Specifically, this section was important:

API enablement and quotas are managed by the master Application Default Credentials project. In the case of errors related to the API not being enabled or quota limitations, use the --client-id-file flag. You can create the client-id-file at https://console.cloud.google.com/apis/credentials.

The command I ended up running:

gcloud auth application-default login --client-id-file=my_app_client_id.json

This will save the Application Default Credentials to a file in your home directory. For example: /Users/nathanb/.config/gcloud/application_default_credentials.json

Nathan B
  • 21
  • 4