0

I have created a Dialogflow V2 agent and am trying to integrate the same with ionic 4 App.

Steps Followed:

  1. Created a service account and created a JSON key file from google console.
  2. Added the path to ENV variables GOOGLE_APPLICATION_CREDENTIALS
  3. Also ran the below commands for gcloud authentication
    1. gcloud auth application-default login
    2. gcloud auth application-default print-access-token
  4. I can even able to create the access token using the google jwt token

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.

I have followed the below link to create the Ionic App https://www.yuribacciarini.com/integrate-dialogflow-chatbot-on-ionic-app/

API CALL:

I am using Native HTTP call

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 data Response

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. Thanks & Regards, Janani

Janani
  • 29
  • 1
  • 1
  • 3

1 Answers1

0

The blog you are following is out dated.

To communicate with dialogflow API you need to have access token.

You can follow API v2 guide,

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

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
  • Yes I have generated the access token. Also, am following the same steps as in the blog which you have shared. Am trying to make the http call with the detectintent API. But now am getting 403 access denied issue. **IAM permission 'dialogflow.sessions.detectIntent' on 'projects/*/agent' denied.**". I have given **Dialogflow API Admin** role for the members in cloud console IAM. Even though am getting access denied issue. Help me on the same. – Janani Jul 27 '20 at 05:06
  • may be you have misconfigured something – Nikhil Savaliya Jul 27 '20 at 09:41
  • I am using **ionic native http api call** to get the response from detect intent api. But the data in response returned as a single string Eg : **{ data:"querytext:etc.,"}. if I parse the data also am getting error. Even I tried with **angular http api call** but with that getting **preflight cors issue** in Mobile App. Can you give me any solution for this. – Janani Aug 01 '20 at 07:01
  • Hi Updated the issue and added my code snippet above. Please check and help me to solve this issue. – Janani Aug 02 '20 at 17:55
  • you are not passing headers, please follow blog step by step – Nikhil Savaliya Aug 03 '20 at 06:57