javascript - 上传 Intent 函数 Dialogflow V2

标签 javascript node.js dialogflow-es

我正在尝试开发一个 API 以将意图上传到 Dialogflow V2。我已经尝试了下面的代码片段,但它不起作用,但是如果尝试与 Dialogflow 进行通信,它确实有效(检测意图)并且确实从 Dialogflow 获得了查询回复。

权限

我是管理员 > 服务帐户 > DIALOGFLOW 管理员

错误

Error: 7 PERMISSION_DENIED: IAM permission 'dialogflow.entityTypes.create' on 'projects/dexter-47332/agent' denied.

博客/引用资料

  1. Dialogflow easy way for authorization
  2. https://github.com/dialogflow/dialogflow-nodejs-client-v2/blob/master/samples/resource.js#L26
  3. https://www.npmjs.com/package/dialogflow
  4. https://developers.google.com/apis-explorer/
  5. https://cloud.google.com/docs/authentication/production

//------- keys.json (test 1)

{
  "type": "service_account",
  "project_id": "mybot",
  "private_key_id": "123456asd",
  "private_key": "YOURKEY",
  "client_email": "yourID@mybot.iam.gserviceaccount.com",
  "client_id": "098091234",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/yourID%40mybot.iam.gserviceaccount.com"
}


//--------------------- ** (test 2) ** ---------

let privateKey = 'key';
let clientEmail = "email";

let config = {
  credentials: {
    private_key: privateKey,
    client_email: clientEmail
  }
}

function createEntityTypes(projectId) {
  // [START dialogflow_create_entity]
  // Imports the Dialogflow library
  const dialogflow = require('dialogflow');

  // ******** Instantiates clients (Test 1)********
  const entityTypesClient = new dialogflow.EntityTypesClient({
    'keyFilename': './keys.json'
  });
  const intentsClient = new dialogflow.IntentsClient({
    'keyFilename': './keys.json'
  });

  // ******** Instantiates clients (Test 2)********
  const entityTypesClient = new dialogflow.EntityTypesClient(config);
  const intentsClient = new dialogflow.IntentsClient(config);


  // The path to the agent the created entity type belongs to.
  const agentPath = intentsClient.projectAgentPath(projectId);

  const promises = [];

  // Create an entity type named "size", with possible values of small, medium
  // and large and some synonyms.
  const sizeRequest = {
    parent: agentPath,
    entityType: {
      displayName: 'test',
      kind: 'KIND_MAP',
      autoExpansionMode: 'AUTO_EXPANSION_MODE_UNSPECIFIED',
      entities: [{
          value: 'small',
          synonyms: ['small', 'petit']
        },
        {
          value: 'medium',
          synonyms: ['medium']
        },
        {
          value: 'large',
          synonyms: ['large', 'big']
        },
      ],
    },
  };
  promises.push(
    entityTypesClient
    .createEntityType(sizeRequest)
    .then(responses => {
      console.log('Created size entity type:');
      logEntityType(responses[0]);
    })
    .catch(err => {
      console.error('Failed to create size entity type ----->:', err);
    })
  );
}

createEntityTypes(projectId);

最佳答案

您可以使用 JWT(JSON Web token )对服务帐户进行身份验证,如 example

 const serviceAccount = { };    // JSON key contents {"type": "service_account",...

 const serviceAccountAuth = new google.auth.JWT({
 email: serviceAccount.client_email,
 key: serviceAccount.private_key,
 scopes: 'https://www.googleapis.com/auth/calendar'
});

有关 Google API 的更多 OAuth2.0 范围,您可以查看完整列表 here .

关于javascript - 上传 Intent 函数 Dialogflow V2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52038315/

相关文章:

javascript - 如何在播放按钮效果上复制愤怒的小鸟鼠标悬停

javascript - 如何使用 ionic 和 firebase 获取注册用户的 uid

php - 获取Facebook当前位置和公开资料的家乡

javascript - 无论用户在 dialogflow v2 中输入什么,如何强制检测特定意图

javascript - 使用 "Next 3 images"制作按钮

javascript - 如何将按钮添加到无状态组件中?

node.js - 如何在 NestJS 中从查询中获取参数

javascript - express 返回帖子信息由另一个功能处理

node.js - 将 @Sys.Any 实体用于我的聊天机器人/助理服务(设计问题)

google-cloud-platform - 我正在使用 GCP 服务帐户,但在调用 Dialog Flow API 时出现错误