NodeJS 使用npm程序调用dialogflow API失败

fhity93d  于 2023-04-20  发布在  Node.js
关注(0)|答案(1)|浏览(122)

我尝试运行一个脚本,通过使用Dialogflow API创建一个特定的Intent。该API已经启用了几周。
当我调用脚本时,我收到以下错误:
错误:Dialogflow API以前未在项目usable-auth-library中使用过或已禁用。请通过访问https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=usable-auth-library启用它,然后重试。如果您最近启用了此API,请等待几分钟,让操作传播到我们的系统,然后重试。
at /Users/$USER”/Desktop/node_modules/grpc/src/client.js:554:15代码:7,
元数据:
元数据{
_internal_repr:
{ 'google.rpc. help-bin':【阵列】,
'grpc-status-details-bin':【阵列】,
'grpc-server-stats-bin':[数组] } } }
这是我的剧本。

// Imports the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiates clients
const contextsClient = new dialogflow.ContextsClient();
const intentsClient = new dialogflow.IntentsClient();
projectId="1000-1505-ecom-conx-dev"
projectId="conrad-test"
// The path to identify the agent that owns the created intent.
const agentPath = intentsClient.projectAgentPath(projectId);

// Setup intents for ordering a pizza.

// First of all, let's create an intent that triggers pizza order flow.

// Output contexts for ordering pizza. They are used for matching follow-up
// intents. For pizza ordering intents, a "pizza" output context is used for
// hinting the conversation is about pizza ordering, not beer or something
// else. For the first intent, it returns responses asking users to provide
// size information, with a "size" output context for matching the intent
// asking for the size of the pizza.

// Note that session ID is unknown here, using asterisk.
const accountOutputContexts = [
  {
    name: contextsClient.contextPath(
      projectId,
      '*' /* sessionId */,
      'EComerceAgent'
    ),
    lifespanCount: 5,
  },
];

// The result of the matched intent.
const accountResult = {
  action: '',
  parameters: [
    {
      displayName: 'Account for',
      value: '$application',
      entityTypeDisplayName: '@application',
      mandatory: true,
      prompts: [
        'You need application access, please describe for which and which permissions do you need?',
        'Would you like access to jirra?',
        'Would you like access to confluence?',
        'Would you like access to AEM?',
      ],
    },
    {
      displayName: 'user',
      value: '$user',
      entityTypeDisplayName: '@user',
      mandatory: true,
      prompts: ['For which user'],
      isList: true,
    },
    {
      displayName: 'permission',
      value: '$permission',
      // The API provides a built-in entity type @sys.address for addresses.
      entityTypeDisplayName: 'permission',
      mandatory: true,
      prompts: ['Which permission do you need?'],
    },
  ],
  messages: [
    {
      text: {
        text: [
          'No problem. We will create an account on $application for $user with the following permission: $permission'
        ],
      },
    },
    {
      text: {
        text: [
          'Reply "check" to place your order. Reply "cancel" to cancel ' +
            'your order. You can change your delivery address as well.',
        ],
      },
    },
    {
      quickReplies: {
        title:
          'No problem. We will create an account on $application for $user with the following permissions: $permission', 
        quickReplies: ['Create account', 'Cancel']
      },
      platform: 'PLATFORM_UNSPECIFIED',
    },
  ],
  outputContexts: accountOutputContexts,
};

// The phrases for training the linguistic model.
const accountPhrases = [
  {type: 'TYPE_EXAMPLE', parts: [{text: 'Get  account'}]},
  {type: 'TYPE_EXAMPLE', parts: [{text: 'acction'}]},
  {
    type: 'TYPE_EXAMPLE',
    parts: [
      {text: 'Create an account '},
      {text: 'for', entityType: '@application', alias: 'application'},
      {text: ' '},
      {text: 'for ', entityType: '@user', alias: 'user'},
      {text: 'with the following permissions ', entityType: '@permission', alias: 'permission'},
    ],
  },
  {
    type: 'TYPE_EXAMPLE',
    parts: [
      {text: "I'd like to have access "},
      {text: 'to', entityType: '@application', alias: 'application'},
    ],
  }
];

// The intent to be created.
const accountIntent = {
  displayName: 'Account',
  events: ['create_account'],
  // Webhook is disabled because we are not ready to call the webhook yet.
  webhookState: 'WEBHOOK_STATE_DISABLED',
  trainingPhrases: accountPhrases,
  mlEnabled: true,
  priority: 500000,
  result: accountResult,
};

const accountRequest = {
  parent: agentPath,
  intent: accountIntent,
};

// Create the pizza intent
intentsClient
  .createIntent(accountRequest)
  .then(responses => {
    console.log('Created account intent:');
    logIntent(responses[0]);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });
dbf7pr2w

dbf7pr2w1#

这个错误根本不清楚,它意味着你没有被授权访问远程资源而没有凭据。下面是我的快速解决方案:
1.转到https://console.cloud.google.com/apis/credentials/serviceaccountkey
1.下载json auth文件(例如foobar-123.json
1.添加环境变量:
export GOOGLE_APPLICATION_CREDENTIALS="/home/me/secure/foobar-123.json”
完整的教程和文档在这里:https://cloud.google.com/docs/authentication/getting-started
我报告了这个问题:https://github.com/dialogflow/dialogflow-nodejs-client-v2/issues/28

相关问题