Imgur API图像搜索未返回数据

Imgur API image search not returning data

本文关键字:返回 数据 搜索 API 图像 Imgur      更新时间:2023-09-26

我正在尝试使用Node.js和Express.js编写一个应用程序(如果这是有用的信息,我正在学习免费代码营课程),该应用程序接收搜索术语,并向Imgur API发送和查询,并报告与搜索术语相关的图像的一些数据。我已经注册了我的应用程序,收到了必要的客户端ID,并在这里的文档中包含了一个标题,我从Imgur获得了200的statusCode,但我在返回的对象中获得了许多其他无用的属性,而不是任何"数据"。我需要做什么才能根据搜索词从Imgur获得图像数据?

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

搜索功能:

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

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

请尝试此url,

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

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

我希望这能有所帮助。对不起,太晚了。

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

替换<lt;client_id>>和您的15个字符的客户端id。

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