DialogFlow v2 访问令牌无法生成 [英] DialogFlow v2 Access Token cannot generate

查看:21
本文介绍了DialogFlow v2 访问令牌无法生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第 1 版中,这就是我过去与 DialogFlow Api 通信的方式!

With version 1 this is how I used to communicate with DialogFlow Api!

fetch(configs.baseUrl + "query?v=20150910", {
    body: JSON.stringify({query: text, lang: "en", sessionId: "somerandomthing"}),
    headers: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + configs.accessToken,
    },
    method: 'POST',
})
    .then(response => response.json())
    .then(data => {
        console.log(data.result.fulfillment.speech);
        return data.result.fulfillment.speech;
    })
    .catch(error => console.error(error))

我只需将访问令牌传递到标头中即可!

I simply had to pass the access token into header and that was it!

我不知道如何使此代码与 DialogFlow v2 一起使用,我被困在访问令牌上,我的 V2 代理之一我无法再看到访问令牌,但我有一个项目 ID 和服务帐户.

I dont know how can I make this code work with DialogFlow v2, I am getting stuck on the access token, one my V2 Agents I can not longer see access token but instead I have a Project Id and Service Account.

我设法从谷歌控制台创建服务密钥并通过 gcloud 激活,但我只是不知道从哪里获取或如何生成此访问令牌,或者我是否需要一个访问令牌进入 v2,如果没有,我该如何处理这是?

I manage to create Service key from google console and activate thru gcloud but I just dont know where to get or how to generate this access token, or do I need an access token into v2, if not, how do I deal with this?

一个工作示例将不胜感激.

A working example would much appreciated.

注意我已经下载了这个包含这些数据的文件,并在 gcloud 中使用了这个文件,它说服务激活了 smth 但是然后呢?这就是全部?我接下来应该做什么,以便我可以对 V2 DialogFlow 进行 http 调用.

Note I have downloaded this file which contains these kind of data and used this file in gcloud and it said that service activated smth but then what? is that all? what should I do next so I can make http call to V2 DialogFlow.

{
  "type": "service_account",
  "project_id": "xxxx",
  "private_key_id": "xxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----xxxx",
  "client_email": "xxxx",
  "client_id": "xxxx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dialogflow-client%40xxxx"
}

推荐答案

下面是使用 Node.js 创建 DialogFlow V2 访问令牌的另一个示例.以下代码中使用的库是 google-oauth-jwt..>

Below is another example of creating your DialogFlow V2 access token using Node.js. The library that is used in the code below is google-oauth-jwt.

const googleAuth = require('google-oauth-jwt');

function generateAccessToken() {
 return new Promise((resolve) => {
  googleAuth.authenticate(
   {
    email: <client_email>,
    key: <private_key>,
    scopes: 'https://www.googleapis.com/auth/cloud-platform',
   },
   (err, token) => {
    resolve(token);
   },
  );
 });
}

您可以从您从 Google Cloud Platform 项目的服务帐户页面下载的 JSON 密钥文件中找到您的 client_emailprivate_key.如果您不确定如何/在哪里下载它,您可以查看我的博客文章 这里.

You may find your client_email and private_key from the JSON key file you have downloaded from your Google Cloud Platform project's service account page. If you are unsure how/where to download it, you may checkout my blog post here.

要了解您可能需要哪个范围,您可以查看 DialogFlow V2 REST API 文档 页面.

To find out which scope which scope you may need, you may checkout the DialogFlow V2 REST API documentation page.

这篇关于DialogFlow v2 访问令牌无法生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆