【问题标题】:Can't get response from imgur api无法从 imgur api 获得响应
【发布时间】:2017-11-28 04:09:16
【问题描述】:

我正在尝试从 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'});
      });
    })

【问题讨论】:

    标签: node.js express xmlhttprequest router axios


    【解决方案1】:

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

    【讨论】:

    • 当然,这就是我对客户端 ID 所做的,但我也无法让它工作。
    • 当它应该在标题中时,您试图将其作为查询发送。
    【解决方案2】:

    将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);
    });
    

    【讨论】:

    • 是的,就是这样。谢谢。
    猜你喜欢
    • 2013-08-02
    • 2020-08-25
    • 2022-11-23
    • 2020-05-08
    • 2023-01-04
    • 2021-09-04
    • 2016-10-05
    • 2016-03-18
    相关资源
    最近更新 更多