0
votes

im using axios to connect my bot to dialog-flow API v1.it is returning a 401 error saying unauthorized?

i have all ready set up the headers and the data types to application/json.

var axios =require('axios');
var URL ='https://api.dialogflow.com/v1/';

let config = {
    headers: {
        "Authorization": "Bearer " + '4c52dfb9db61xxxxxxxxxxxxx',
        "Content-Type": "application/json"

    }
  }
var bodyParameters = {
    "queryInput": { "text":
     { } 
    },
    "query": "hi hello",
    "languageCode": "en",
    "sessionId": "12345",
    "timezone": "xxxxx"
};


axios.get(URL,bodyParameters, config)
.then(function(res){
    console.log(res.data);

}).catch(function(error){
    console.log(error);

});

is there some error in authorization?

1
Check this link. It might help youMuhammad Tayyab Shafique
thanks i finally did it using node-fetch. **for anyone else interested here is my code ** ` fetch(URL+"query?v=20150910", { body: JSON.stringify({query: "new york city", lang: "en", sessionId: "12345"}), headers: { 'content-type': 'application/json', "Authorization": "Bearer " + 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))`marper96

1 Answers

1
votes

after searching i found that it was much easy to do API request using node-fetch

fetch(URL+"query?v=20150910", {
    body: JSON.stringify({query: "new york city", lang: "en", sessionId: "12345"}),
    headers: {
        'content-type': 'application/json',
        "Authorization": "Bearer " + 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))