从 youtube 数据 api v2 更新到 v3 后正确输出视频名称和信息 [英] Output Videoname and infos correctly after update from youtube data api v2 to v3

查看:62
本文介绍了从 youtube 数据 api v2 更新到 v3 后正确输出视频名称和信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你们中的一些人已经解决了这个问题.从 youtube 数据 api v2 更新到 v3 后,我遇到了缩略图、名称和视频持续时间的输出问题.我希望有一种简单的方法,实际上他只给了我一个未受保护的文本,并且在一个对象显示的链接中向上.这是到目前为止的代码,确实需要一些帮助,谢谢.

//YouTube 数据 API 基本 URL(JSON 响应)var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=50&key=XXXXXXXXXX"$.getJSON(url + "&q=" + q, function (json) {无功计数 = 0;如果(json.items){var items = json.items;var html = "";items.forEach(功能(项目){//包括 YouTube 观看 URL youtu.behtml += '<p><a href="https://youtu.be/' + item.id + '">';//添加默认视频缩略图(默认质量)html += '<img src="https://i.ytimg.com/vi/' + item.id + '/default.jpg">';//添加视频标题和时长html += '

'+ item.title + ' ' + item.duration + '</h2></a></p>';计数++;});}

解决方案

你只需要做一些改变:

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

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

<块引用>

网址:https://www.googleapis.com/youtube/v3/videos

检查这个 YouTube 数据 API 中的示例 - 官方文档.

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

<代码>{"kind": "youtube#videoListResponse","etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/8jeHCUnghfiSuxYeP5D9KDIE44Q\"",页面信息":{总结果":1,每页结果":1},项目": [{"kind": "youtube#video","etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/wMjiWCECcoho_QWk9FLayO8Ipus\"","id": "kJQP7kiw5Fk",内容详细信息":{"持续时间": "PT4M42S","dimension": "2d",定义":高清","caption": "真实",许可内容":真,投影":矩形"}}]}

这是我为获取 (videoIdtitle) 值所做修改的代码:

//在此处使用您自己的 API KEY:var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=5&key=YOUR_API_KEY"//这里的例子将搜索关于africa"的结果:$.getJSON(url + "&q=africa", function(json) {无功计数 = 0;var html = "";如果(json.items){var items = json.items;items.forEach(function(item) {//包括 YouTube 观看 URL youtu.behtml += '<p><a href="https://youtu.be/' + item.id.videoId + '">';//添加默认视频缩略图(默认质量)html += '<img src="https://i.ytimg.com/vi/' + item.id.videoId + '/default.jpg">';//添加视频标题和时长//注意,不幸的是,响应中没有持续时间"属性/值.html += '

'+ item.snippet.title + ' - 持续时间:在响应中不可用</h2></a></p>';计数++;});}//将结果添加到名为myDiv"的 div 中.document.getElementById('myDiv').innerHTML = html;})

结果如下:

<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">托托 - 非洲(官方音乐视频)- 时长:不适用</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 歌词 - 时长:不适用</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 - 非洲(由古怪的 Al Yankovic 主演)- 时长:不适用</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)(2010 FIFA World Cup™ 官方歌曲)- 时长:不适用</</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">托托 - 非洲(屏幕上的歌词)- 时长:不适用</a></h2><p></p>

I hope some of you got already experience with this problem. After Update from youtube data api v2 to v3 i got a problem with the output of the thumbnails and names and video durations.I hope there is an easy way, actualy he only gives me an text with undefiened and in the links an Object is showing up. this is the code so far really need some help thanks.

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

        });
      }

解决方案

You only have to do a few changes:

  • Instead of (item.title), use (item.snippet.title).
  • Instead of (item.id), use (item.id.videoId).
  • Unfortunately, the duration property/value is not available in the response.

If you want the "duration" of a video, you have to use the (Videos) API.

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

Check this sample in the YouTube Data API - official documentation.

This is the response of the sample above (you'll see the "duration" property and value):

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

This is the code with the modifications I made for get the (videoId and title) values:

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

And the results are as follows:

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

这篇关于从 youtube 数据 api v2 更新到 v3 后正确输出视频名称和信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆