Imgur API图片搜索未返回数据

时间:2016-05-05 18:12:48

标签: javascript imgur

我尝试使用Node.js和Express.js编写应用程序(我正在通过免费代码营课程,如果这是有用的信息),它会占用搜索字词并发送和查询Imgur API并报告有关搜索词的图像的一些数据。我已经注册了我的申请并收到了必要的客户ID,并且根据这里的文档包含了一个标题,我从Imgur获得了200的statusCode,但是没有任何"数据",我&#39 ; m在返回的对象中获得了许多其他无用的属性。如何根据搜索词从Imgur获取图像数据?

我将请求发送到的网址: 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

根据文档,图像数据应该在&#34;数据&#34;属性,不会返回上述属性。

3 个答案:

答案 0 :(得分:1)

请尝试这个网址,

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

答案 1 :(得分:0)

尝试使用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))

我希望有所帮助。对不起,如果为时已晚。

答案 2 :(得分:0)

fields = ['Name','Gender','State','Credit Card','Phone','Spent Past 6 Months'] 
print('|'.join((row[field] for field in fields)))

用您的15个字符的客户ID替换<< client_id >>。

例如: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();