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

时间:2019-03-21 17:24:09

标签: google-api dialogflow google-oauth2

这是他们的示例代码:

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

我一生中从未见过像dialogflow这样的不负责任的团队

更新:

导出env变量解决了我的问题,现在我从dialogflow中得到了一些东西,但没有达到预期,可能是因为他们的存储库样本代码中的代码不正确

这是他们作为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不存在,它给了我未定义的

更新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]

 ...

2 个答案:

答案 0 :(得分:1)

google auth库正在寻找一个名为 指向JSON凭证文件的GOOGLE_APPLICATION_CREDENTIALS。

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

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

答案 1 :(得分:0)

您尝试过看看吗?您需要设置身份验证,将服务帐户密钥创建为.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"});