javascript - 403 IAM 权限,尽管授予代理管理员权限以授予列出意图的权限
问题描述
我想使用我用 Dialogflow 创建的聊天机器人作为我网站的主页。google.api_core.exceptions.PermissionDenied: 403 IAM permission 'dialogflow.sessions.detectIntent' on 'projects/pollingagent-jnscpa/agent' denied.
然而,当我试图与他交谈时,它告诉我。
我按照本教程讲述了如何使用 Flask 进行操作,但我已准备好转向最简单的解决方案。我从类似问题的答案中尝试了以下内容:
- 在 Dialogflow 的控制台中,转到设置⚙ > 在常规选项卡下,项目 ID 部分带有一个 Google Cloud 链接,用于打开 Google Cloud 控制台 > 打开 Google Cloud。
- 在谷歌云中,转到“成员”选项卡下的 IAM Admin > IAM。找到我的代理人的名字,然后点击编辑。
- 向代理授予管理员权限以授予列出意图的权限。
所以我不知道,也许问题出在我的 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()
解决方案
您可能面临以下两个问题之一:
- 您可能没有正确设置 json 凭证文件的路径。您可以将其设置为环境变量或执行
os.environ['GOOGLE_APPLICATION_CREDENTIALS']="/path/to/project_name-xxxxxxxx.json"
- 转到 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))
推荐阅读
- java - 即使在成功加载所需的目标文件后,在加载 JNI 依赖项时也会在 Java 中出现 UnsatisfiedLinkError(未定义符号)
- r - 将数据框与缺失值相结合
- node.js - 使用节点/快速应用程序时,Heroku open 不起作用
- c++ - 矢量与动态数组基准测试 - 瓶颈是什么?
- java - 你能检查一个文件是否真的是Java中的图像吗?
- validation - 如何使用异步验证器测试 Angular 4 字段验证
- c# - 单个班级项目中的可访问性级别
- jquery-ui-datepicker - Datepicker jQuery 不更新 ngModel AngularJS 4
- javascript - CSS & Javascript 将 rotateY 转换为元素的背面
- reactjs - bundle.js 未呈现 / 404 bundle.js 未找到