Imgur API image search not returning data

I'm trying to write an app using Node.js and Express.js (I'm working through the Free Code Camp curriculum, if that's useful information) that takes in a search term and sends and inquiry to the Imgur API and report back some data about images related to the search term. I've registered my application and received the necessary Client-ID and have included a header per the documentation here and am getting a statusCode of 200 from Imgur, but instead of any "data", I'm getting a number of other properties in the returned object that are not useful. What do I need to do to get image data from Imgur based on a search term?

The URL I'm sending the request to: https://api.imgur.com/3/gallery/search/{search term} per the documentation here: https://api.imgur.com/endpoints/gallery#gallery-search. The response is not in the format described

Search function:

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

The output:

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

Based on the documentation, the image data should be in a "data" property, is not returned with the above properties.


请尝试此网址,

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

try using node-fetch , I personally find the API of it a lot easier to use. So go ahead and npm i node-fetch . Then try the code below:

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

I hope that helps. Sorry if it's too late.

链接地址: http://www.djcxy.com/p/81446.html

上一篇: 使用Imgur API进行身份验证

下一篇: Imgur API图像搜索不返回数据