我希望你们中的一些人已经对这个问题有经验。从youtube data api v2更新到v3后,缩略图,名称和视频时长的输出出现问题。我希望有一个简单的方法,实际上他只给了我未定义的文本,并且在对象显示的链接中起来到目前为止,这是代码,确实需要一些帮助。
// YouTube Data API base URL (JSON response)
var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=50&key=XXXXXXXXXX"
$.getJSON(url + "&q=" + q, function (json) {
var count = 0;
if (json.items) {
var items = json.items;
var html = "";
items.forEach(function (item) {
// Include the YouTube Watch URL youtu.be
html += '<p><a href="https://youtu.be/' + item.id + '">';
// Add the default video thumbnail (default quality)
html += '<img src="https://i.ytimg.com/vi/' + item.id + '/default.jpg">';
// Add the video title and the duration
html += '<h2>' + item.title + ' ' + item.duration + '</h2></a></p>';
count++;
});
}
答案 0 :(得分:0)
您只需要做一些更改:
item.title
),而不是(item.snippet.title
)。item.id
),而不是(item.id.videoId
)。duration
属性/值在响应中不可用。如果要视频的“持续时间”,则必须使用(视频)API。
选中此sample in the YouTube Data API - official documentation。
这是上方示例的响应(您将看到“ duration”属性和值):
{
"kind": "youtube#videoListResponse",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/8jeHCUnghfiSuxYeP5D9KDIE44Q\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/wMjiWCECcoho_QWk9FLayO8Ipus\"",
"id": "kJQP7kiw5Fk",
"contentDetails": {
"duration": "PT4M42S",
"dimension": "2d",
"definition": "hd",
"caption": "true",
"licensedContent": true,
"projection": "rectangular"
}
}
]
}
这是经过修改以获取(videoId
和title
)值的代码:
// Use your own API KEY here:
var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=5&key=YOUR_API_KEY"
// The example here will search results about "africa":
$.getJSON(url + "&q=africa", function(json) {
var count = 0;
var html = "";
if (json.items) {
var items = json.items;
items.forEach(function(item) {
// Include the YouTube Watch URL youtu.be
html += '<p><a href="https://youtu.be/' + item.id.videoId + '">';
// Add the default video thumbnail (default quality)
html += '<img src="https://i.ytimg.com/vi/' + item.id.videoId + '/default.jpg">';
// Add the video title and the duration
// N.B, unfortunately, the "duration" property/value is not available in the response.
html += '<h2>' + item.snippet.title + ' - Duration: not available in the response</h2></a></p>';
count++;
});
}
// Add the results in the div called "myDiv".
document.getElementById('myDiv').innerHTML = html;
})
结果如下:
<div id="myDiv">
<p>
<a href="https://youtu.be/FTQbiNvZqaY"><img src="https://i.ytimg.com/vi/FTQbiNvZqaY/default.jpg"></a>
</p>
<h2><a href="https://youtu.be/FTQbiNvZqaY">Toto - Africa (Official Music Video) - Duration: N/A</a></h2>
<p></p>
<p>
<a href="https://youtu.be/DWfY9GRe7SI"><img src="https://i.ytimg.com/vi/DWfY9GRe7SI/default.jpg"></a>
</p>
<h2><a href="https://youtu.be/DWfY9GRe7SI">Toto Africa Lyrics - Duration: N/A</a></h2>
<p></p>
<p>
<a href="https://youtu.be/mk5Dwg5zm2U"><img src="https://i.ytimg.com/vi/mk5Dwg5zm2U/default.jpg"></a>
</p>
<h2><a href="https://youtu.be/mk5Dwg5zm2U">Weezer - Africa (starring Weird Al Yankovic) - Duration: N/A</a></h2>
<p></p>
<p>
<a href="https://youtu.be/pRpeEdMmmQ0"><img src="https://i.ytimg.com/vi/pRpeEdMmmQ0/default.jpg"></a>
</p>
<h2><a href="https://youtu.be/pRpeEdMmmQ0">Shakira - Waka Waka (This Time for Africa) (The Official 2010 FIFA World Cup™ Song) - Duration: N/A</a></h2>
<p></p>
<p>
<a href="https://youtu.be/qDLJ3pUZm9A"><img src="https://i.ytimg.com/vi/qDLJ3pUZm9A/default.jpg"></a>
</p>
<h2><a href="https://youtu.be/qDLJ3pUZm9A">Toto - Africa (Lyrics on screen) - Duration: N/A</a></h2>
<p></p>
</div>