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

查看:57
本文介绍了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.

我设法从Google控制台创建服务密钥并通过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);
   },
  );
 });
}

您可能会找到 client_email private_key 从您从Google Cloud Platform项目的服务帐户页面下载的JSON密钥文件中。如果不确定如何下载/在何处下载,可以查看我的博客文章

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天全站免登陆