Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting with Dialogflow V2 api using Python

As mentioned in the documentation, I have setup cloud account along with activating the dialogflow API. Then I activated dialogflow V2 in my dialogflow.com account along with setting the same google project in it.

I downloaded the google credentials in JSON format and set it's path for authentication too accordingly.

After doing all this, when I ran

from google.cloud import storage
storage_client = storage.Client()
buckets = list(storage_client.list_buckets())
print(buckets)

It gave me the error,

OSError: Project was not passed and could not be determined from the environment.

So, I set the project name in the storage client itself.

storage_client = storage.Client(project='[Project-id]')

So, that solved the problem with the code, still, I think it should have detected the project-id on its own without having to provide the project id as has been given in the documentation.

After all this, I tried running the below program, to check the connection,

import dialogflow_v2beta1
client = dialogflow_v2beta1.AgentsClient()
parent = client.project_path('[Project-id]')
response = client.import_agent(parent)

def callback(operation_future):
    result = operation_future.result()

response.add_done_callback(callback)
metadata = response.metadata()

I am getting the following error after this,

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

Any help would be appreciated.

like image 663
Apurv Anand Avatar asked Jan 08 '18 06:01

Apurv Anand


2 Answers

Please Set the environment variable GOOGLE_APPLICATION_CREDENTIALS https://cloud.google.com/dialogflow-enterprise/docs/reference/libraries

Examples:

export GOOGLE_APPLICATION_CREDENTIALS="/home/thanhha/owner-project-ab.json"
like image 185
Thanh Ha Avatar answered Oct 05 '22 07:10

Thanh Ha


Once we create the Service account Key (which is a json file, lets rename it as private_key.json) as per the instructions from https://dialogflow.com/docs/reference/v2-auth-setup?authuser=2

We need to set this in the environment variable as @ThanhHa had mentioned. Another alternative to setting the environment variable through the terminal, is, adding the following code to your python code.

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'private_key.json'

This solved my problem.

like image 23
Sachin PC Avatar answered Oct 05 '22 07:10

Sachin PC