我创建了一个带有并发 gcloud 应用程序的 Dialogflow 代理。但是,当我尝试将其集成到 Node.js 后端时,它似乎访问了错误的应用程序默认凭据。
我可以确认它正在验证身份验证是否成功,因为它在我运行此代码时控制台记录了正确的项目 ID (diagnosistest-56e81):
// Imports the Google Cloud client library.
const Storage = require('@google-cloud/storage')
// Instantiates a client. If you don't specify credentials when constructing
// the client, the client library will look for credentials in the
// environment.
const storage = new Storage();
// Makes an authenticated API request.
storage
.getBuckets()
.then((results) => {
const buckets = results[0];
console.log('Buckets:');
buckets.forEach((bucket) => {
console.log(bucket.name);
});
})
.catch((err) => {
console.error('ERROR:', err);
})
但是,当我尝试连接 dialogflow 时,出现错误。
连接到对话流的代码:
// You can find your project ID in your Dialogflow agent settings
const projectId = 'diagnosistest-56e81'; //https://dialogflow.com/docs/agents#settings
const sessionId = 'quickstart-session-id';
const query = 'hello';
const languageCode = 'en-US';
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();
// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
// Send request and log result
sessionClient
.detectIntent(request)
.then(responses => {
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.`);
}
})
.catch(err => {
console.error('ERROR:', err);
});
我收到的错误消息:
ERROR: { Error: 7 PERMISSION_DENIED: Dialogflow API has not been used in project 764086051850 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/dialogflow.googleapis.com/overview?project=764086051850 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
奇怪的是,我的项目编号是 889800549397 而不是 764086051850。所以它似乎没有访问正确的凭据。
到目前为止我已经尝试过:
- 通过创建新的 Dialogflow 服务帐户密钥并将导出设置为 json 密钥文件
export GOOGLE_APPLICATION_CREDENTIALS='/Users/Joseph/workspace/finddoc/DiagnosisTest-cred.json
- 删除
application_default_credentials.json
文件./config/gcloud/
夹中的文件。
当我这样做时,我收到一条不同的错误消息:
ERROR: Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.