【问题标题】:Imgur API image search not returning dataImgur API图像搜索不返回数据
【发布时间】:2016-08-31 15:30:44
【问题描述】:

我正在尝试使用 Node.js 和 Express.js 编写一个应用程序(我正在学习免费代码营课程,如果这是有用的信息),它接收搜索词并发送和查询到 Imgur API并报告一些与搜索词相关的图像数据。我已经注册了我的应用程序并收到了必要的 Client-ID 并在此处的文档中包含了一个标头,并且从 Imgur 获得了 200 的状态代码,但我没有获得任何“数据”,而是获得了许多其他属性返回的没有用的对象。我需要做什么才能根据搜索词从 Imgur 获取图像数据?

我将请求发送到的 URL: https://api.imgur.com/3/gallery/search/{search term} 根据此处的文档:https://api.imgur.com/endpoints/gallery#gallery-search。响应不是所描述的格式

搜索功能:

this.askImgur = function(req, res) {
  var img_data;
  console.log('askImgur function firing');
  var search_term = url.parse(req.originalUrl||req.path).query;
  console.log(search_term);
  var search_path = path + search_term;//PLUS PAGE

  var options = {
    protocol: "https:",
    host:'api.imgur.com',
    path:search_path,
    method:'GET',
    headers: {
    "Authorization":"Client-ID <CLIENT ID HERE>"
    }
  };

var ds;
  https.get(options, function(res) {
    console.log("Got response: " + res.statusCode);
    for (var key in res) {
      if (res.hasOwnProperty(key)) {
      console.log(key);
      }
    }
  }).on('data', function(chunk){
    ds+=chunk;
    console.log("chunk is "+chunk);//does nothing
  }).on('error', function(e) {
    console.log("Got error: " + e.message);
  });
  console.log("ds is "+ds);//does nothing
  //res.json("askImgur function is sending you words");
  res.send(search_term);
};//askImgur function

输出:

Got response: 200 _readableState readable domain _events _eventsCount _maxListeners socket connection httpVersionMajor httpVersionMinor httpVersion complete headers rawHeaders trailers rawTrailers upgrade url method statusCode statusMessage client _consuming _dumped req

根据文档,图像数据应该在“数据”属性中,不与上述属性一起返回。

【问题讨论】:

    标签: javascript imgur


    【解决方案1】:

    请试试这个网址,

    https://api.imgur.com/3/gallery/search?q={search term}
    

    【讨论】:

      【解决方案2】:

      尝试使用node-fetch,我个人觉得它的API 更容易使用。所以继续npm i node-fetch。 然后试试下面的代码:

      const fetch = require("node-fetch")
      
      const term = "lolcat"
      const url = `https://api.imgur.com/3/gallery/search/top/1/?q=${term}`
      const IMGUR_API_CLIENT = "111111" // your client api
      
      fetch(url, {headers: {Authorization: `Client-ID ${IMGUR_API_CLIENT}`}})
        .then(res => res.json())
        .then(json => console.log(json))
      

      我希望这会有所帮助。抱歉,如果为时已晚。

      【讨论】:

        【解决方案3】:
        private OkHttpClient httpClient = new OkHttpClient.Builder().build();
        
        Request request = new Request.Builder()
        .url("https://api.imgur.com/3/gallery/hot/viral/0.json")
        .method("GET", null)
        .addHeader("Authorization", "Client-ID <<client_id>>")
        .build();
        

        将 > 替换为您的 15 个字符的客户 ID。

        例如:".addHeader(Authorization", "Client-ID a1b2c3d4e5f6g7h")

        【讨论】:

          猜你喜欢
          • 2017-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多