简体   繁体   English

如何在 ionic App 中使用 Dialogflow Api.ai V2

[英]How to use Dialogflow Api.ai V2 in ionic App

I have created a Dialogflow V2 agent and am trying to integrate the same with ionic 4 App.我创建了一个 Dialogflow V2 代理并尝试将其与 ionic 4 App 集成。

Steps Followed :遵循的步骤

  1. Created a service account and created a JSON key file from google console.创建了一个服务帐户并从谷歌控制台创建了一个 JSON 密钥文件。
  2. Added the path to ENV variables GOOGLE_APPLICATION_CREDENTIALS添加了 ENV 变量 GOOGLE_APPLICATION_CREDENTIALS 的路径
  3. Also ran the below commands for gcloud authentication还运行以下命令进行 gcloud 身份验证
    1. gcloud auth application-default login gcloud auth 应用程序-默认登录
    2. gcloud auth application-default print-access-token gcloud auth 应用程序默认打印访问令牌
  4. I can even able to create the access token using the google jwt token我什至可以使用谷歌 jwt 令牌创建访问令牌

Here the problem is when I use the same access token in my ionic app or even in the postman, am getting 401 unauthorized Invalid access token error .这里的问题是,当我在我的 ionic 应用程序甚至 postman 中使用相同的访问令牌时,我收到401 authorized Invalid access token error

I have followed the below link to create the Ionic App https://www.yuribacciarini.com/integrate-dialogflow-chatbot-on-ionic-app/我按照以下链接创建了 Ionic App https://www.yuribacciarini.com/integrate-dialogflow-chatbot-on-ionic-app/

API CALL: API 致电:

I am using Native HTTP call我正在使用本机 HTTP 调用

import { HTTP } from '@ionic-native/http/ngx';

 private nativeHttp: HTTP

let encUrl = "https://dialogflow.googleapis.com/v2/projects/id/agent/sessions/id:detectIntent";

  return from(this.nativeHttp.post("URL", data, headers)
        .then(res => {   
          console.log(res); //Returning [object object]
           console.log(JSON.stringify(res)); //Returning data as single string
          return res;
        }, (error: any) => {
          return this.handleError(error);
        }));

Response:回复:

API 数据响应

In this response data returned as string.在此响应中,数据以字符串形式返回。 I have tried to parse the data and if I take response.data.queryResult or Response.queryResult it shows undefined .我试图解析数据,如果我采用response.data.queryResult 或 Response.queryResult 它显示 undefined Thanks & Regards, Janani谢谢和问候,贾纳尼

The blog you are following is out dated.您关注的博客已过时。

To communicate with dialogflow API you need to have access token.要与对话流 API 进行通信,您需要拥有访问令牌。

You can follow API v2 guide,您可以遵循 API v2 指南,

https://chatbotslife.com/dialogflow-v2-rest-api-communication-6cf7ab66ab36 https://chatbotslife.com/dialogflow-v2-rest-api-communication-6cf7ab66ab36

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM