【问题标题】:How authenticate with gcloud credentials an Dialogflow API如何使用 gcloud 凭据对 Dialogflow API 进行身份验证
【发布时间】:2018-05-15 16:54:19
【问题描述】:

我有一个向 Dialogflow 代理发出请求的 Node JS 应用程序。我实际上使用了一个基于临时令牌的请求,但是我怎样才能通过谷歌服务凭证来改变它呢? (https://cloud.google.com/docs/authentication/getting-started)。我创建了一个凭证(添加了计费)和 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);

包的示例使用项目 ID 和会话 ID,但没有像 google 服务示例那样使用 json 文件(或使用像 How to authenticate with gcloud big query using a json credentials file? 这样的大查询)。无论如何,我在哪里可以得到这个项目和会话 ID?

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

【问题讨论】:

  • 我已经使用服务帐户完成了此操作,但如果这对您有帮助,则使用 Java/kotlin,那么我可以分享代码.. 我确定 JS 也有类似的东西
  • 是的,请务必!

标签: node.js dialogflow-es service-accounts


【解决方案1】:

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

  • 方法一

    创建一个环境变量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
    
  • 方法二

    假设,您知道 JSON 文件的绝对路径,并将其作为值放在 credentials_file_path 变量的 sn-p 下方。

    // 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,
    });
  • 方法3

    您可以记下 JSON 中的 project_idclient_emailprivate_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,
});

【讨论】:

  • 非常感谢!!,但是我从哪里得到“const sessionId = 'fa2d5904-a751-40e0-a878-d622fa8d65d9';”? json 文件带有以下值:type、project_id、private_key_id、private_key、c​​lient_email、client_id、auth_uri、token_uri、auth_provider_x509_cert_url 和 client_x509_cert_url。
  • @Sebastián session id 你可以设置任何你想区分用户聊天的任意值。从 json 文件中,您只考虑 project_id、client_email 和 private_key,如果您使用方法 3
  • 如何测试和运行该方法 3 ?我们应该做http请求吗??
【解决方案2】:

这里是如何使用 kotlin 中的服务帐户代码示例来实现的,并且绝对可以翻译成 node.js sdk

    val credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials
            .fromStream(Classes.getResourceAsStream([YOUR JSON CONFIG FILE GOES HERE])))
    val sessionsSettings = SessionsSettings.newBuilder().setCredentialsProvider(credentialsProvider).build()
    sessionsClient = SessionsClient.create(sessionsSettings)

您可以从 Dialogflow 设置中获取服务帐户,单击服务帐户链接,然后在您的云控制台中创建一个 json 配置文件。

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2020-07-13
    • 2020-12-22
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    相关资源
    最近更新 更多