凭据进行身份验证

凭据进行身份验证

本文介绍了如何使用Dialogflow API使用gcloud凭据进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node JS应用,该应用向Dialogflow代理发出请求。我实际上使用的是基于时间令牌的请求,但是如何通过Google服务凭据更改此请求呢? ()。我创建了一个凭证(添加了帐单)和service_account json文件。



我想在节点中使用Dialogflow包(),但我并不理解如何将其与json结合使用文件。

  const projectId =‘ENTER_PROJECT_ID_HERE’; 
const sessionId ='quickstart-session-id';
const query ='hello';
const languageCode ='en-US';

//实例化DialogFlow客户端。
const dialogflow = require('dialogflow');
const sessionClient =新的dialogflow.SessionsClient();

//定义会话路径
const sessionPath = sessionClient.sessionPath(projectId,sessionId);

该包的示例使用Project ID和Session ID,但不使用json文件,例如Google服务(或使用大查询,例如)。无论如何,我在哪里可以获得这个项目和会话ID?



请,如果有人可以帮助我或指导如何以更好的方式做到这一点?谢谢

解决方案

首先,您必须创建一个服务帐户并下载 .JSON 格式的凭据文件在您的本地系统上。
现在,有三种方法可以使用这些凭据在dialogflow库中进行身份验证/授权。




  • 方法1



    创建环境变量 GOOGLE_APPLICATION_CREDENTIALS ,其值应为该JSON凭据文件的绝对路径。 ,Google库将隐式加载文件并使用该凭据进行身份验证。我们无需在代码中进行与此凭证文件有关的任何操作。

      export GOOGLE_APPLICATION_CREDENTIALS =<绝对路径-of-json-file> #对于UNIX,LINUX 
    #然后运行您的代码,Google库将选择凭据文件并自动加载


  • 方法2



    假设您知道JSON文件的绝对路径,并将其作为值放在下面的代码段中 credentials_file_path 变量。

      //您可以在Dialogflow代理设置中找到您的项目ID 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凭据文件路径='<<绝对文件路径-JSON文件'; //实例化一个DialogFlow客户端。 > 


  • 方法3



    您可以记下JSON中的 project_id client_email private_key ,并在代码中明确使用它们进行身份验证。




  //您可以找到您的项目ID在您的Dialogflow代理设置中const projectId ='< project-id-here>';; const sessionId ='< put-chat-session-id-here>'; // const se ssionid ='fa2d5904-a751-40e0-a878-d622fa8d65d9'const查询='hi'; const languageCode ='zh-CN'; const凭证= {client_email:'< client-email-here>',private_key:'< ;; private-key-here>',}; //实例化一个DialogFlow客户端。const dialogflow = require('dialogflow'); const sessionClient = new dialogflow.SessionsClient({projectId,凭据,});  


I have a Node JS app that make requests to a Dialogflow agent. I actually use a temporally token based request, but how can i change this to do it through google service credentials? (https://cloud.google.com/docs/authentication/getting-started). I have a credencial created (with billing added), and the service_account json file.

I would like to use the Dialogflow package in node (https://www.npmjs.com/package/dialogflow) but i don't underestand how to use it with the json file.

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);

The example of the package use Project ID and Session ID, but not with a json file like the example of the google services (or using big query like How to authenticate with gcloud big query using a json credentials file?). Anyway, where can i get this project and session id?

Please, if someone can help me or guide how to do this in a better way?. Thanks

解决方案

First you have to create a service account and download a .JSON format file of credentials on your local system.Now, there are three ways to use that credentials for authentication/authorisation in dialogflow library.

  • Method 1

    Create a environment variable GOOGLE_APPLICATION_CREDENTIALS and it's value should be the absolute path of that JSON credentials file.By this method, google library will implicitly loads the file and use that credentials for authentication. We don't need to do anything inside our code relating to this credentials file.

    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
    

  • Method 2

    Assume, you know the absolute path of your JSON file and put that as value in below snippet of credentials_file_path variable.

    // 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,
    });

  • Method 3

    You can note down the project_id, client_email and private_key from the JSON, use them in your code for authentication explicitly.

// 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,
});

这篇关于如何使用Dialogflow API使用gcloud凭据进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:52