Dialogflow NodeJS 知识 IAM 权限

Dialogflow NodeJS Knowledge IAM permission

我一直在关注 GitHub samples 以创建、列出文档并将其添加到 Dialogflow 的知识库。但是使用这些 NodeJS 示例时,我收到了需要身份验证的错误。当我尝试添加一些会话(基于 Dialogflow 的常规会话客户端)时,我得到 IAM 权限被拒绝。

我如何在我的本地 NodeJS 环境中以编程方式测试这些示例?

以下代码要求我进行身份验证

async function listKnowledgeBases(projectId) {
    // [START dialogflow_list_knowledge_base]
    // Imports the Dialogflow client library
    const dialogflow = require('dialogflow').v2beta1;

    // Instantiate a DialogFlow KnowledgeBasesClient.
    const client = new dialogflow.KnowledgeBasesClient({
        projectPath: projectId,
    });

    const formattedParent = client.projectPath(projectId);

    const [resources] = await client.listKnowledgeBases({
        parent: formattedParent,
    });

    resources.forEach(r => {
        console.log(`displayName: ${r.displayName}`);
        console.log(`name: ${r.name}`);
    });

    // [END dialogflow_list_knowledge_base]
}

错误

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/Users/c024323/Documents/Workspace/JSWorkspace/HelpCenterPOC/node_modules/google-gax/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async GoogleAuth.getClient (/Users/c024323/Documents/Workspace/JSWorkspace/HelpCenterPOC/node_modules/google-gax/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
    at async GrpcClient._getCredentials (/Users/c024323/Documents/Workspace/JSWorkspace/HelpCenterPOC/node_modules/google-gax/build/src/grpc.js:92:24)
    at async GrpcClient.createStub (/Users/c024323/Documents/Workspace/JSWorkspace/HelpCenterPOC/node_modules/google-gax/build/src/grpc.js:213:23)

以下代码给出了 IAM 权限被拒绝的错误

const createKnowledgeBase = async (projectId, displayName) => {
    // [START dialogflow_create_knowledge_base]
    // Imports the Dialogflow client library
    const dialogflow = require('dialogflow').v2beta1;
    const sessionId = require('uuid/v1')();

    let config = {
        credentials: {
            private_key: service_key.private_key,
            client_email: service_key.client_email
        }
    };

    // // Create a new session
    // const sessionClient = new dialogflow.SessionsClient(config);
    // const sessionPath = sessionClient.sessionPath(projectId, sessionId);

    // Instantiate a DialogFlow client.
    const client = new dialogflow.KnowledgeBasesClient(config);


    const formattedParent = client.projectPath(projectId);
    const knowledgeBase = {
        displayName: displayName,
    };
    const request = {
        parent: formattedParent,
        knowledgeBase: knowledgeBase,
    };

    const [result] = await client.createKnowledgeBase(request);
    console.log(`Name: ${result.name}`);
    console.log(`displayName: ${result.displayName}`);

    return result;
    // [END dialogflow_create_knowledge_base]
};

错误

{"code":7,"details":"IAM permission 'dialogflow.knowledgeBases.create' on 'projects/XXXXX' denied.","metadata":{"internalRepr":{},"options":{}}}

问题出在凭据密钥上。它是给客户的,而不是给管理员的。创建管理员角色密钥后,我就可以创建知识库了。