首页 > 解决方案 > 403 IAM 权限,尽管授予代理管理员权限以授予列出意图的权限

问题描述

我想使用我用 Dialogflow 创建的聊天机器人作为我网站的主页。google.api_core.exceptions.PermissionDenied: 403 IAM permission 'dialogflow.sessions.detectIntent' on 'projects/pollingagent-jnscpa/agent' denied. 然而,当我试图与他交谈时,它告诉我。

我按照本教程讲述了如何使用 Flask 进行操作,但我已准备好转向最简单的解决方案。我从类似问题的答案中尝试了以下内容:

在此处输入图像描述

所以我不知道,也许问题出在我的 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()

标签: javascriptpython-3.xpermissionsdialogflow-es

解决方案


您可能面临以下两个问题之一:

  1. 您可能没有正确设置 json 凭证文件的路径。您可以将其设置为环境变量或执行os.environ['GOOGLE_APPLICATION_CREDENTIALS']="/path/to/project_name-xxxxxxxx.json"
  2. 转到 dialogflow 主页,然后单击显示的位置在此处输入图像描述

然后单击 PROJECT_ID 链接转到您的谷歌云控制台

进入谷歌云控制台后,打开显示的侧面菜单面板,然后转到 IAM & Admin > IAM

在此处输入图像描述

您应该看到所有成员,如果角色未设置为所有者,则单击编辑图标并将角色更改为所有者

在此处输入图像描述

通过以下步骤,我能够运行以下代码段

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))

推荐阅读