无法获得imgur api的回复

时间:2017-06-24 20:30:49

标签: node.js express xmlhttprequest router axios

我试图从imgur api得到回应,而我却无法得到任何东西。我一直得到401未经授权的错误。我也曾尝试使用".../gallery/authorization?client_id=###########/search="+req.body.search,但这也不起作用。我真的不了解文档。谁能告诉我我做错了什么?哦,顺便说一句,这是用express / node.js编写的。

 router.post('/getimages', function(req,res,next){
      console.log('successfully got to /getimages backend :P');
      console.log('the value of the search query is ', req.body.search);
      var url = 'https://api.imgur.com/3/gallery/search?q='+req.body.search;
      axios.get(url)
      .then(function (response) {
        console.log('YATA from axios to imgur');
        console.log(response);
          res.json({'YATA':'YATA from getimages'});
      })
      .catch(function (error) {
        console.log('Fail from axios request to imgur');
        console.log(error);
          res.json({'OHNOES':'NOOOOYATA from getimages'});
      });
    })

2 个答案:

答案 0 :(得分:0)

根据the documentation,您必须register your app才能使用API​​。即使您将以匿名身份使用它,这似乎也是允许的。

答案 1 :(得分:0)

clientId放在授权标题中:

axios = require("axios");

search = "monkey";
clientId = "YOUR_CLIENT_ID";

axios({
    method: 'get',
    url: 'https://api.imgur.com/3/gallery/search?q=' + search,
    headers: { 'authorization': 'Client-ID ' + clientId }
}).then(function(response) {
    console.log(response.data);
}).catch(function(error) {
    console.log(error);
});