我创建了服务帐户,并按照本指南提供给我的环境
https://cloud.google.com/dialogflow/cx/docs/quick/setup#windows
我尝试使用firebase serve运行我的代码,但得到了以下错误:
Error: 7 PERMISSION_DENIED: IAM permission 'dialogflow.sessions.detectIntent' on 'projects/botDialogflowCX/locations/us-central1/agents/chat' denied
我确信服务帐户是正确的。我已经尝试创建了dialogflow管理员帐户、客户端和项目所有者帐户。
以下是我的代码
const functions = require("firebase-functions");
const { SessionsClient } = require("@google-cloud/dialogflow-cx");
const crededentials = require("../../.env/botdialogflowcx-5e936a89c163.json");
exports.teste = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", { structuredData: true });
const client = new SessionsClient({
apiEndpoint: "us-central1-dialogflow.googleapis.com",
});
const sessionId = Math.random().toString(36).substring(7);
const sessionPath = client.projectLocationAgentSessionPath(
"botDialogflowCX",
"us-central1",
"chat",
sessionId);
console.info(sessionPath);
const requestDialogflow = {
session: sessionPath,
queryInput: {
text: {
text: "Oi",
},
languageCode: "pt-br",
},
};
client.detectIntent(requestDialogflow).then((snapshot) => {
const webhookResponse = {
fulfillment_response: {
messages: [{
text: {
text: ["testandoooo", snapshot],
},
},
],
},
};
response.send(webhookResponse);
}).catch((error) => {
console.log(error);
response.status(500).send(error);
});
});我真的不知道发生了什么。
运行命令
gcloud projects get-iam-policy botdialogflowcx --flatten="bindings[].members" --format="table(bindings.role)" --filter="bindings.members:teste-889@botdialogflowcx.iam.gserviceaccount.com"
输出是roles/dialogflow.admin。
我将电子邮件添加到对话框CX - agent - share中的服务帐户。
email in the dialogflow CX - agent - share
但是仍然有相同的错误,即IAM没有权限。
发布于 2021-08-12 21:38:58
出现IAM权限被拒绝错误通常是因为您正在使用的服务帐户没有被授予足够的权限,无法对连接到Dialogflow Agent的GCP项目执行所请求的操作,或者您在请求中使用了错误的凭据,或者您查询了错误的代理。
查看以下代码和遇到的错误,似乎使用了Project Name和Agent Name,而不是分别使用Project ID和Agent ID值。
const sessionPath = client.projectLocationAgentSessionPath(
"botDialogflowCX", // update to Project ID
"us-central1",
"Chat", // update to Agent ID
sessionId);请注意,项目ID和代理ID与项目名称和代理名称不同,您可以参考how to collect IDs上的以下文档。
发布于 2021-08-12 17:09:44
这样啊,原来是这么回事。我只需要改变
client.projectLocationAgentSessionPath(
"botDialogflowCX",
"us-central1",
"chat",
sessionId);至
const sessionPath = client.projectLocationAgentSessionPath(
"botdialogflowcx",
"us-central1",
"e55b9ef5-d1f2-4e5c-9e95-974501233d50",
sessionId);而且它起作用了。
发布于 2021-08-11 22:56:00
如果您在云函数中运行此代码,我认为您不需要提供凭据。如果你在本地运行,你可以这样设置你的凭证:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
这样,您就不需要在代码中提供凭据。
https://stackoverflow.com/questions/68749649
复制相似问题