2

I have a Node JS app that make requests to a Dialogflow agent. I actually use a temporally token based request, but how can i change this to do it through google service credentials? (https://cloud.google.com/docs/authentication/getting-started). I have a credencial created (with billing added), and the service_account json file.

I would like to use the Dialogflow package in node (https://www.npmjs.com/package/dialogflow) but i don't underestand how to use it with the json file.

const projectId = 'ENTER_PROJECT_ID_HERE'; 
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 example of the package use Project ID and Session ID, but not with a json file like the example of the google services (or using big query like How to authenticate with gcloud big query using a json credentials file?). Anyway, where can i get this project and session id?

Please, if someone can help me or guide how to do this in a better way?. Thanks

Sebastián
  • 95
  • 1
  • 8

2 Answers2

13

First you have to create a service account and download a .JSON format file of credentials on your local system. Now, there are three ways to use that credentials for authentication/authorisation in dialogflow library.

  • Method 1

    Create a environment variable GOOGLE_APPLICATION_CREDENTIALS and it's value should be the absolute path of that JSON credentials file.By this method, google library will implicitly loads the file and use that credentials for authentication. We don't need to do anything inside our code relating to this credentials file.

    export GOOGLE_APPLICATION_CREDENTIALS="<absolute-path-of-json-file>" # for UNIX,LINUX
    # then run your code, google library will pick credentials file and loads it automatically
    
  • Method 2

    Assume, you know the absolute path of your JSON file and put that as value in below snippet of credentials_file_path variable.

    // You can find your project ID in your Dialogflow agent settings
    const projectId = '<project-id-here>';
    const sessionId = '<put-chat-session-id-here>'; 
    // const sessionid = 'fa2d5904-a751-40e0-a878-d622fa8d65d9'
    const query = 'hi';
    const languageCode = 'en-US';
    const credentials_file_path = '<absolute-file-path-of-JSON-file>';
    
    // Instantiate a DialogFlow client.
    const dialogflow = require('dialogflow');
    
    const sessionClient = new dialogflow.SessionsClient({
      projectId,
      keyFilename: credentials_file_path,
    });
  • Method 3

    You can note down the project_id, client_email and private_key from the JSON, use them in your code for authentication explicitly.

// You can find your project ID in your Dialogflow agent settings
const projectId = '<project-id-here>';
const sessionId = '<put-chat-session-id-here>';
// const sessionid = 'fa2d5904-a751-40e0-a878-d622fa8d65d9'
const query = 'hi';
const languageCode = 'en-US';
const credentials = {
  client_email: '<client-email-here>',
  private_key:
    '<private-key-here>',
};
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');

const sessionClient = new dialogflow.SessionsClient({
  projectId,
  credentials,
});
Akshay Pratap Singh
  • 3,197
  • 1
  • 24
  • 33
  • Thanks a lot!!, but from where i get the "const sessionId = 'fa2d5904-a751-40e0-a878-d622fa8d65d9';"?. The json file come with this values: type, project_id, private_key_id, private_key, client_email, client_id, auth_uri, token_uri, auth_provider_x509_cert_url, and client_x509_cert_url. – Sebastián May 17 '18 at 19:39
  • 1
    @Sebastián session id you can set any arbitrary value which you want to distinguish a user chat. From json file, you only consider project_id, client_email and private_key only, if you are using method 3 – Akshay Pratap Singh May 18 '18 at 03:07
  • how to test and run that method 3 ? should we do http request ?? – poppop Jul 12 '19 at 03:10
1

Here is how you can do it with a service account code sample is in kotlin and definitely can be translated into the node.js sdk

    val credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials
            .fromStream(Classes.getResourceAsStream([YOUR JSON CONFIG FILE GOES HERE])))
    val sessionsSettings = SessionsSettings.newBuilder().setCredentialsProvider(credentialsProvider).build()
    sessionsClient = SessionsClient.create(sessionsSettings)

You can get the service account from Dialogflow settings click on the service account links and then create a json config file there in ur cloud console.

N Jay
  • 1,774
  • 1
  • 17
  • 36