Reputation: 3296
I want to use the chatbot I created with Dialogflow as the main page of my website. Yet it tells me that google.api_core.exceptions.PermissionDenied: 403 IAM permission 'dialogflow.sessions.detectIntent' on 'projects/pollingagent-jnscpa/agent' denied.
when trying to talk to him.
I followed this tutorial that tells how to do it with Flask but I'm ready to shift to the easiest solution. I tried the following thing from the answers to a similar question:
So I don't know, maybe the problem is from my index.py?
from flask import Flask, request, jsonify, render_template
import os
import dialogflow
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def detect_intent_texts(project_id, session_id, text, language_code):
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
if text:
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
return response.query_result.fulfillment_text
@app.route('/send_message', methods=['POST'])
def send_message():
message = request.form['message']
project_id = os.getenv('DIALOGFLOW_PROJECT_ID')
fulfillment_text = detect_intent_texts(project_id, "unique", message, 'en')
response_text = { "message": fulfillment_text }
return jsonify(response_text)
# run Flask app
if __name__ == "__main__":
app.run()
Upvotes: 1
Views: 1158
Reputation: 405
There can be one of the two issues which you might be facing:
os.environ['GOOGLE_APPLICATION_CREDENTIALS']="/path/to/project_name-xxxxxxxx.json"
Then click on PROJECT_ID link to goto your google cloud console
After you are in google cloud console, open the side menu panel as displayed and then goto IAM & Admin > IAM
You should see all the members and if the role is not set to Owner then click on edit icon and change role to owner
With the following steps I was able to run my the following snippet
from google.cloud import dialogflow
import random
import string
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "\path\to\<project_name>-xxxxxx.json"
x = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16))
client_session = dialogflow.SessionsClient()
session = client_session.session_path("<project_name>", x)
print(session)
text = input("Please enter a text : ")
text_input = dialogflow.TextInput(text=text, language_code='en')
query_input = dialogflow.QueryInput(text=text_input)
response = client_session.detect_intent(
request={'session': session, 'query_input': query_input})
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(response.query_result.fulfillment_text))
Upvotes: 2
Reputation: 3332
You haven't setup the credentials in the code, do you have the environment variable set for credentials?
The GOOGLE_APPLICATION_CREDENTIALS
variable is the easiest way to do this, where the value of that variable is set to the path to the service account key that you download.
You can do this in the code as well if you want, so it doesn't affect the rest of your environment with something like:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'path/to/private_key.json'
Upvotes: 1