DialogFlow V1 to V2, GCP Authentication and Accessing APIs by 3 ways.

As everybody knows this warning for dialogFlow V1 version:

The warning was from dialogFlow website before 23 OCT.
Warning: V1 of dialogFlow’s API will be shut down on October 23, 2019.

After 23 OCT its:

Caution: Your project will have an existing service account, but this should not be altered. For additional client and developer API access, you should create a new service account.

DialogFlow Docs: https://dialogflow.com/docs/reference/v2-auth-setup

So our venerated client hoisted disquiet over Dialogflow API version v1 and to get version v2 as early as possible in production before 23 October. As there was less information on the internet and this V2 is under the Google Cloud Platform (GCP). Our team was stimulated to go through GCP. Our thinking was GCP authentication will be like self-explanatory functionality flow but it was very immense.

So to convert Dialogflow v1 to v2 was having less information source, so we decided to write this article, which may help others.

There are 3 ways to implement V2 :

Environment variable ‘Google_Application_Credentia’ is an ENV for every API request, GCP authenticates it for user/account service(same as a ‘Bearer token’ for ajax call).

  1. Using libraries/packages without ENV var & SDK installation,
  2. Using libraries/packages using ENV var & without SDK installation,
  3. By installing Google SDK (using ENV var)

So, firstly we started with creating a new agent in Dialogflow (Agent is created under dialogFlow v2)

  1. Created the agent — default is V2 now.
  2. Enabled V2 beta features.
  3. Downloaded private key JSON file from the GCP service account.

Private key JSON file is same as a user-access token to authenticate the API request in version V1. Private key is for credential verification for the specific GCP account.

  1. Below is the downloaded private key JSON file (.json)

{
“type”: “<>”, “project_id”: “<>”, “private_key_id”: “<>”, “private_key”: “ <>”, “client_email”: “<>”, “client_id”: “<>”, “auth_uri”: “<>",
“token_uri”: “<>", “auth_provider_x509_cert_url”: “<>",
“client_x509_cert_url”: “<>"
}

* Using libraries/packages with/without ENV var & SDK installation :

For installing libraries, you can install via composer, for Nodejs via ‘ npm I dialogflow

Import all client libraries/packages needed for CRUD API operation.

Authentication libraries:
Google\Cloud\Storage\StorageClient

Dialogflow libraries:

use Google\Cloud\Dialogflow\V2\SessionsClient;

use Google\Cloud\Dialogflow\V2\QueryInput;

use Google\Cloud\Dialogflow\V2\TextInput;

use Google\Cloud\Dialogflow\V2\CreateIntentRequest;

use Google\Cloud\Dialogflow\V2\Intent;

use Google\Cloud\Dialogflow\V2\IntentsClient;

use Google\Cloud\Dialogflow\V2\sessionPath;

use Google\Cloud\Dialogflow\V2\RequestParamsHeaderDescriptor;

use Google\Cloud\Dialogflow\V2\Intent_TrainingPhrase_Part;

use Google\Cloud\Dialogflow\V2\Intent_TrainingPhrase;

use Google\Cloud\Dialogflow\V2\Intent_Message_Text;

use Google\Cloud\Dialogflow\V2\Intent_Message;

Setting Credentials(Mandatory)

Set downloaded Private key JSON file path in ENV var ‘GOOGLE_APPLICATION_CREDENTIALS

putenv function is just one way to set an environment variable.

Get Specific INTENT

List All INTENTS

Delete INTENT

Create New INTENT

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

* By installing Google SDK (ENV var is important) :

  1. Install Google SDK
  2. Set downloaded private key JSON file in system properties/Advanced/Environment Variables/System Variables.
  3. To get Access Token run this ‘ gcloud auth application-default login’ with your dialogflow account service and then run ‘gcloud auth application-default print-access-token’ in CMD, you will get the token and set it in Request Header(Authorization) in ajax call
  4. Below are the all CRUD APIs (AJAX call)

Create New INTENT

Update Specific INTENT

List All INTENTS

To get specific INTENT

To delete specific INTENT

So finally you will get the result for migrating DialogFlow V1 to V2.
This article will help you to implement using Google SDK and with/without ENV variable for libraries/packages.

(Full Stack Developer) CoReCo Technologies