Imgur API图像搜索未返回数据
Imgur API image search not returning data
我正在尝试使用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")
- AJAX调用并在Node中获取返回数据
- 角度服务未返回数据
- 统计ajax返回数据中的html元素
- 有没有更好的方法可以测量从Ajax调用返回数据所需的总时间
- Imgur API图像搜索未返回数据
- javascript对象显示为null,但object.properties返回数据
- 如何从ajax调用JQuery返回数据
- 试图从嵌套的回调函数返回数据
- NodeJS FS 不从多个文件返回数据
- ajax函数未在C#中返回数据
- 如何在公共js模块中从web服务返回数据
- 将字符串与 JSON 对象组合以返回数据
- 嵌套异步函数未及时返回数据的问题
- NodeJS JSONStream 以一个长字符串的形式返回数据
- 如何使用 HTML 图像请求将数据发送到服务器,或返回数据作为响应
- 从 Angular 模态服务中的模态主体模板返回数据
- jQuery 每个循环返回数据两次
- $.getJSON 在返回数据后不会运行函数
- 如何访问返回数据中的特定json值
- 工厂返回数据后未设置角度$scope