我创建了一个带有并发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);
})
但是,当我尝试连接对话框流时,出现错误。
连接到对话框流的代码:
// 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.所以看起来它没有访问正确的凭据。
到目前为止我尝试了什么:
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.
答案 0 :(得分:1)
最后找出了问题所在。设置环境变量GOOGLE_APPLICATION_CREDENTIALS后,您必须重新启动计算机才能生效!
答案 1 :(得分:0)
在尝试使用Dialogflow V2 API时,我在本地计算机上遇到了完全相同的错误消息(包括764086051850项目ID)。我不想麻烦GOOGLE_APPLICATION_CREDENTIALS
环境变量,所以我遵循了here的说明。
特别是,此部分很重要:
API启用和配额由主“应用程序默认凭据”项目管理。如果出现与未启用API或配额限制相关的错误,请使用--client-id-file标志。您可以在https://console.cloud.google.com/apis/credentials上创建客户端ID文件。
我最终运行的命令:
gcloud auth application-default login --client-id-file=my_app_client_id.json
这会将“应用程序默认凭据”保存到主目录中的文件中。例如:/Users/nathanb/.config/gcloud/application_default_credentials.json