难以从dialogflow api v1获得授权。收到错误消息“请求失败,状态码为401”

时间:2019-03-24 11:37:54

标签: node.js axios dialogflow

我正在使用axios将我的机器人连接到dialog-flow API v1。它返回401错误,提示未授权?

我已经准备好将标题和数据类型设置为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);

});

授权中有错误吗?

1 个答案:

答案 0 :(得分:1)

搜索后,我发现使用node-fetch进行API请求非常容易

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))