Dialogflow v2客户端库不工作|错误:无法加载默认凭据

问题描述 投票:0回答:2

这是他们的示例代码:

const dialogflow = require('dialogflow');
const uuid = require('uuid');

/**
 * Send a query to the dialogflow agent, and return the query result.
 * @param {string} projectId The project to be used
 */
async function runSample(projectId = 'your-project-id') {
  // A unique identifier for the given session
  const sessionId = uuid.v4();

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

  // The text query request.
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        // The query to send to the dialogflow agent
        text: 'hello',
        // The language used by the client (en-US)
        languageCode: 'en-US',
      },
    },
  };

  // Send request and log result
  const responses = await sessionClient.detectIntent(request);
  console.log('Detected intent');
  const result = responses[0].queryResult;
  console.log(`  Query: ${result.queryText}`);
  console.log(`  Response: ${result.fulfillmentText}`);
  if (result.intent) {
    console.log(`  Intent: ${result.intent.displayName}`);
  } else {
    console.log(`  No intent matched.`);
  }
}

并且此代码根本不起作用,给出错误:无法加载默认凭据:

2019-03-21T16:59:40.099101+00:00 app[web.1]: Message: hi Bilal
2019-03-21T16:59:40.102561+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
2019-03-21T16:59:40.102565+00:00 app[web.1]:     at GoogleAuth.<anonymous> (/app/web/node_modules/google-auth-library/build/src/auth/googleauth.js:168:23)
2019-03-21T16:59:40.102568+00:00 app[web.1]:     at Generator.next (<anonymous>)
2019-03-21T16:59:40.102570+00:00 app[web.1]:     at fulfilled (/app/web/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58)
2019-03-21T16:59:40.102572+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:68:7)
2019-03-21T16:59:40.102691+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
2019-03-21T16:59:40.102784+00:00 app[web.1]: (node:23) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2019-03-21T16:59:55.986568+00:00 app[web.1]: Message: hi Bilal
2019-03-21T16:59:55.986595+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
2019-03-21T16:59:55.986598+00:00 app[web.1]:     at GoogleAuth.<anonymous> (/app/web/node_modules/google-auth-library/build/src/auth/googleauth.js:168:23)
2019-03-21T16:59:55.986600+00:00 app[web.1]:     at Generator.next (<anonymous>)
2019-03-21T16:59:55.986602+00:00 app[web.1]:     at fulfilled (/app/web/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58)
2019-03-21T16:59:55.986605+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:68:7)
2019-03-21T16:59:55.986647+00:00 app[web.1]: (node:23) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

他们有如何在回购中使用这个库的说明,但它没有意义https://github.com/googleapis/nodejs-dialogflow

他们没有在任何地方描述如何在调用检测意图时放置凭据,而不是在示例代码中的前夕:https://github.com/googleapis/nodejs-dialogflow/blob/master/samples/detect.js

在我的生活中,我从未见过像对话流团这样不负责任的团队

Update:

导出env变量解决了我的问题,现在我从对话框流程中得到了一些东西,但没有按照预期,可能是他们的存储库示例代码中的代码不正确

这是他们作为try an example所拥有的代码:

  ...

  // Send request and log result
  const responses = await sessionClient.detectIntent(request);
  console.log('Detected intent');
  const result = responses[0].queryResult;
  console.log(`  Query: ${result.queryText}`);
  console.log(`  Response: ${result.fulfillmentText}`);
  if (result.intent) {
    console.log(`  Intent: ${result.intent.displayName}`);
  } else {
    console.log(`  No intent matched.`);
  }

  ...

实际上result.fulfillmentText不存在,它给了我未定义

Update 2

我已经做了很多努力(请参阅下面的console.logs),只是为了理解现在它们返回responses[0].queryResult.fulfillmentText而不是responses[0].queryResult.fulfillmentMessages而不是文本字符串,但它是一个对象,其中包含更多值,您可以在console.logs中看到下面:

...

// Send request and log result
const responses = await sessionClient.detectIntent(request);
console.log('Detected intent');

console.log("responses: ", responses)
console.log("responses[0]: ", responses[0])
console.log("responses[0].queryResult: ", responses[0].queryResult)
console.log("responses[0].queryResult.fulfillmentMessages: ", responses[0].queryResult.fulfillmentMessages)
// console.log("responses[0].queryResult.fulfillmentMessages[1]: ", responses[0].queryResult.fulfillmentMessages[1])
console.log("responses[0].queryResult.fulfillmentMessages[0]: ", responses[0].queryResult.fulfillmentMessages[0])
console.log("responses[0].queryResult.fulfillmentMessages[0].text: ", responses[0].queryResult.fulfillmentMessages[0].text)
console.log("responses[0].queryResult.fulfillmentMessages[0].text.text: ", responses[0].queryResult.fulfillmentMessages[0].text.text)
console.log("responses[0].queryResult.fulfillmentMessages[0].text.text[0]: ", responses[0].queryResult.fulfillmentMessages[0].text.text[0])

var fulfilmentText = responses[0].queryResult.fulfillmentMessages[0].text.text[0]

 ...
google-api dialogflow google-oauth2
2个回答
1
投票

google auth库正在查找名为GOOGLE_APPLICATION_CREDENTIALS的环境变量,该变量指向JSON凭据文件。

您可以按照https://dialogflow.com/docs/reference/v2-auth-setup中所述的说明下载该文件。

错误消息后面的链接提供了如何设置该环境变量的示例:https://cloud.google.com/docs/authentication/getting-started


1
投票

你试过看看这个吗?您需要设置身份验证,创建服务帐户密钥作为.json然后让Google Cloud SDK处理它。

https://dialogflow.com/docs/reference/v2-auth-setup

您也可以尝试传递这样的服务帐户

  // Create a new session
  const sessionClient = new dialogflow.SessionsClient({keyFilename: "./service_account.json"});
© www.soinside.com 2019 - 2024. All rights reserved.