0
votes

I have developed chatbots using Dialogflow api v1. There had been no change in the code. But, now the response from the server is "code":401,"errorType":"unauthorized","errorDetails":"Authentication parameters missing. I checked in Postman using the below url. https://api.dialogflow.com/v1/query?v=519794&lang=en&query=hi&sessionId=519794&Content-Type=application/json and gave my client access token in bearer token. When I tried with post method I got error.But, when i used GET method it worked in postman. So, I tried the same code in my javascript code and got 401 unauthorized error. What's wrong here?

1

1 Answers

0
votes

I have resolved this issue using the following code.

Used GET method instead of POST in Ajax call and removed JSON.stringify in the data parameter.

var text = "hello"; var iRandom = Math.floor((Math.random() * 10000000) + 1);

$.ajax({

       type: "GET",

       url: "https://api.api.ai/v1/query?v=324233",

       contentType: "application/json; charset=utf-8",

       dataType: "json",

       headers: {

       "Authorization": "Bearer "+accessToken

        },

   data: { query: text, lang: "en", sessionId: iRandom},

   success: function(data) {
        //desired action              
         },

       error: function() {

         console.log("Internal Server Error");

      }

   });

Another alternate solution:

I retained the earlier version.

var iRandom = Math.floor((Math.random() * 10000000) + 1);

type: POST, data: JSON.stringify({ query: text, lang: "en", sessionId: iRandom}),

Instead of passing the iRandom as number, I passed it as String and I was able to get response from the dialogflow.