google-api - Dialogflow v2 客户端库不工作 | 错误:无法加载默认凭据
问题描述
这是他们的示例代码:
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)
他们有说明如何在 repo 中使用这个库,但这没有意义https://github.com/googleapis/nodejs-dialogflow
他们没有在任何地方描述如何在调用检测意图时放置凭据,而不是在此处的示例代码中:https ://github.com/googleapis/nodejs-dialogflow/blob/master/samples/detect.js
我这辈子从没见过像Dialogflow团队这样不负责任的团队
更新:
导出 env 变量解决了我的问题,现在我从 dialogflow 中得到了一些反馈,但与预期不同,可能是他们的存储库示例代码中的代码不正确
这是他们作为示例的代码:
...
// 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
不存在,它给了我 undefined
更新 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]
...
解决方案
你试过看看这个吗?您需要设置身份验证,将服务帐户密钥创建为 .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"});
推荐阅读
- python - 计算日志文件的最大并发唯一用户数
- android - SearchView 在滚动之前不会更新 RecyclerView
- python - 带 Gif 图像的 ImDecode
- f# - F# SQLProvider 错误 FS0039:类型“dataContext”未定义字段、构造函数或成员“test”。在演示中
- node.js - 提取 JWT 时请求未定义
- scala - Scala中是否有一些基于条件的类型?
- amazon-web-services - 在 eks 中为服务创建服务端点
- c# - 带有附加参数的 WebApi POST
- git - 如何推送没有文件夹 .gitignore 规则的文件来提供
- python - 为什么 QMainWindow 不显示?