IAM 权限在尝试检测 dialogflow CX 上的意图时被拒绝

IAM permission denied while trying to detect intent on dialogflow CX

我创建了服务帐户,并按照本指南提供给我的环境

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

我将电子邮件添加到 dialogflow CX - agent - share 中的服务帐户。

email in the dialogflow CX - agent - share

email in the account service

但是还是报同样的错误,IAM没有权限。

如果您是 运行 云函数中的此代码,我认为您不需要提供凭据。如果您在本地 运行,您可以像这样设置您的凭据:

$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

这样您就不需要在代码中提供凭据。

我明白了。我只需要更改

client.projectLocationAgentSessionPath(
        "botDialogflowCX",
        "us-central1",
        "chat",
        sessionId);

    const sessionPath = client.projectLocationAgentSessionPath(
        "botdialogflowcx",
        "us-central1",
        "e55b9ef5-d1f2-4e5c-9e95-974501233d50",
        sessionId);

成功了。

IAM Permission denied 错误通常是因为您正在使用的服务帐户未被授予足够的权限来对连接到 Dialogflow Agent 的 GCP 项目执行请求的操作,您在请求中使用了不正确的凭据,或者您查询的代理不正确

查看以下代码和遇到的错误,似乎分别使用了项目名称和代理名称,而不是项目 ID 和代理 ID 值。

const sessionPath = client.projectLocationAgentSessionPath(
        "botDialogflowCX", // update to Project ID
        "us-central1",
        "Chat", // update to Agent ID
        sessionId);

请注意,Project ID和Agent ID不同于Project Name和Agent Name,您可以参考how to collect IDs上的以下文档。