Seb*_*ián 2 node.js service-accounts dialogflow-es
我有一个Node JS应用程序向Dialogflow代理发出请求.我实际上使用基于时间令牌的请求,但我如何通过谷歌服务凭证来改变这一点?(https://cloud.google.com/docs/authentication/getting-started).我创建了一个credencial(添加了计费)和service_account json文件.
我想在节点(https://www.npmjs.com/package/dialogflow)中使用Dialogflow包,但我不知道如何将它与json文件一起使用.
const projectId = 'ENTER_PROJECT_ID_HERE';
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);
Run Code Online (Sandbox Code Playgroud)
该软件包的示例使用项目ID和会话ID,但不使用像google服务示例的json文件(或使用大型查询,如如何使用json凭据文件对gcloud大查询进行身份验证?).无论如何,我在哪里可以得到这个项目和会话ID?
请问,如果有人可以帮助我或指导如何以更好的方式做到这一点?谢谢
首先,您必须创建一个服务帐户并在本地系统上下载凭证的.JSON格式文件.现在,有三种方法可以在dialogflow库中使用该凭据进行身份验证/授权.
方法1
创建一个环境变量GOOGLE_APPLICATION_CREDENTIALS,它的值应该是该JSON凭证文件的绝对路径.通过这种方法,谷歌库将隐式加载该文件并使用该凭据进行身份验证.我们不需要在与此凭证文件相关的代码中执行任何操作.
export GOOGLE_APPLICATION_CREDENTIALS="<absolute-path-of-json-file>" # for UNIX,LINUX
# then run your code, google library will pick credentials file and loads it automatically
Run Code Online (Sandbox Code Playgroud)方法2
假设您知道JSON文件的绝对路径,并将其作为值放在credentials_file_path变量的下面片段中.
// You can find your project ID in your Dialogflow agent settings
const projectId = '<project-id-here>';
const sessionId = '<put-chat-session-id-here>';
// const sessionid = 'fa2d5904-a751-40e0-a878-d622fa8d65d9'
const query = 'hi';
const languageCode = 'en-US';
const credentials_file_path = '<absolute-file-path-of-JSON-file>';
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient({
projectId,
keyFilename: credentials_file_path,
});Run Code Online (Sandbox Code Playgroud)
方法3
您可以记下JSON中的project_id,client_email和private_key,在代码中使用它们进行显式身份验证.
// You can find your project ID in your Dialogflow agent settings
const projectId = '<project-id-here>';
const sessionId = '<put-chat-session-id-here>';
// const sessionid = 'fa2d5904-a751-40e0-a878-d622fa8d65d9'
const query = 'hi';
const languageCode = 'en-US';
const credentials = {
client_email: '<client-email-here>',
private_key:
'<private-key-here>',
};
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient({
projectId,
credentials,
});Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2048 次 |
| 最近记录: |