从youtube数据api v2更新到v3后,正确输出视频名称和信息



我希望你们中的一些人已经有了这个问题的经验。在从youtube数据api v2更新到v3之后,我在缩略图、名称和视频持续时间的输出方面遇到了问题。我希望有一个简单的方法,事实上,他只给了我一条没有好友的文本,并且在链接中显示了一个Object。这是迄今为止真正需要帮助的代码,谢谢。

// 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++;
});
}

您只需要做一些更改:

  • 使用(item.snippet.title(代替(item.title(
  • 使用(item.id.videoId(代替(item.id(
  • 遗憾的是,响应中没有duration属性/值

如果您想要视频的"持续时间",您必须使用(视频(API。

URL:https://www.googleapis.com/youtube/v3/videos

在YouTube数据API官方文档中查看此示例。

这是上面样本的响应(您将看到"持续时间"属性和值(:

{
"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"
}
}
]
}

这是我为获得(videoIdtitle(值而修改的代码:

// 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>

最新更新