iam尝试使用youtube API来查找有关使用YouTube API的特定视频的信息。我使用了HTTPS模块来发送和接收数据
这是我使用的代码
var youtube_query=querystring.stringify({
q:'bangarang',
key:'api_key',
part:'snippet'
});
var options_you = {
host:'www.googleapis.com',
method:'GET',
path:'/youtube/v3/search'
};
function getvid_id(vid_result){
//callback function for finding the information on the video
vid_result.setEncoding('utf8');
console.log("STATUS :"+vid_result.statusCode);//to show the status code
vid_result.on('data', function (body) {
console.log(body);
});
}
var youtube_request = https.request(options_you,getvid_id);
youtube_request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
youtube_request.write(youtube_query);
youtube_request.end();
但是我得到以下响应
STATUS :400
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Required parameter: part",
"locationType": "parameter",
"location": "part"
}
],
"code": 400,
"message": "Required parameter: part"
}
}
您可以看到,我已经提供了零件参数。但是我找不到它不起作用的原因。
您可以直接将Querystring传递到path
字段,例如:
var querystring = require("querystring");
var https = require("https");
var youtube_query = querystring.stringify({
q: 'bangarang',
key: 'api_key',
part: 'snippet'
});
var options_you = {
host: 'www.googleapis.com',
method: 'GET',
path: '/youtube/v3/search?' + youtube_query
};
var youtube_request = https.request(options_you, function(res) {
res.on('data', function(d) {
process.stdout.write(d);
});
});
youtube_request.end();