使用Youtube V3 API和JavaScript获取Youtube视频网址

问题描述:

我已经在这个主题上进行了很多搜索,但是我只能找到与Youtube API的V2相关的资源,这些资源也没有被depricated。使用Youtube V3 API和JavaScript获取Youtube视频网址

我想要做的就是从使用Javascript的Youtube V3 API URL的JSON响应中获取Youtube视频标题,缩略图和链接。

我运行下面的代码,但得到一个错误消息Uncaught TypeError: Cannot read property 'items' of undefined

<input id="searchquery" /> 
<div id="results"></div> 

<!-- Include the latest jQuery library --> 
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 

<script type="text/javascript"> 
jQuery(document).ready(function ($) { 

    $('#searchquery').keyup(function() { 

    // the search term 
    var q = $('#searchquery').val().trim(); 

    // container to display search results 
    var $results = $('#results'); 

    // YouTube Data API base URL (JSON response) 
    var url = "https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&key=AIzaSyC3TJRI1C0EVEdl7b19InH1gv9yrAOX2YU" 


    url = url + '&max-results=50'; 

    $.getJSON(url + "&q=" + q, function (json) { 

     var count = 0; 

     if (json.data.items) { 

     var items = json.data.items; 
     var html = ""; 

     items.forEach(function (item) { 

      // Check the duration of the video, 
      // full-length movies are generally longer than 1 hour 
      var duration = Math.round((item.duration)/(60 * 60)); 

      // Filter out videos that aren't in the Film or Movies category 
      if ((duration > 1) && (item.category == "Movies" || item.category == "Film")) { 

      // Include the YouTube Watch URL youtu.be 
      html += '<p><a href="http://youtu.be/' + item.id + '">'; 

      // Add the default video thumbnail (default quality) 
      html += '<img src="http://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++; 
      } 
     }); 
     } 

     // Did YouTube return any search results? 
     if (count === 0) { 
     $results.html("No videos found"); 
     } else { 
     // Display the YouTube search results 
     $results.html(html); 
     } 
    }); 
    }); 
}); 
</script> 

我的网址是 https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&key=AIzaSyC3TJRI1C0EVEdl7b19InH1gv9yrAOX2YU&q=opencourseware

这将返回

{ 
"kind": "youtube#searchListResponse", 
"etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/anQeVHiFN6CH_dx-tP89Q8heOos\"", 
"nextPageToken": "CAUQAA", 
"pageInfo": { 
    "totalResults": 24815, 
    "resultsPerPage": 5 
}, 
"items": [ 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/4DMiUQkDt0Bic4cV0lZbi7KZ1tk\"", 
    "id": { 
    "kind": "youtube#playlist", 
    "playlistId": "PL-4XVpiAbrw8bRUhUHfm-omUYAyBepYiB" 
    }, 
    "snippet": { 
    "publishedAt": "2015-09-01T06:23:39.000Z", 
    "channelId": "UCOY4GuDhH3KHVcpZqD5S87w", 
    "title": "Popular Videos - Bioinformatics & OpenCourseWare", 
    "description": "", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/R-PjAS8d1_0/default.jpg" 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/R-PjAS8d1_0/mqdefault.jpg" 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/R-PjAS8d1_0/hqdefault.jpg" 
    } 
    }, 
    "channelTitle": "", 
    "liveBroadcastContent": "none" 
    } 
    }, 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/-D3XCQyB-podnhbL-keBQoIZMb8\"", 
    "id": { 
    "kind": "youtube#video", 
    "videoId": "vOpp8nOp9uA" 
    }, 
    "snippet": { 
    "publishedAt": "2015-08-10T09:27:38.000Z", 
    "channelId": "UCBL-6EKd9b1DW73ZcY-PNcw", 
    "title": "Audio OpenCourseWare", 
    "description": "OpenCourseWare dilaksanakan pada tanggal 10 Juli 2015 di Ruang Video Conference, Gedung D, Lantai 2 Direktorat Sistem Informasi, Telkom University ...", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/vOpp8nOp9uA/default.jpg" 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/vOpp8nOp9uA/mqdefault.jpg" 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/vOpp8nOp9uA/hqdefault.jpg" 
    } 
    }, 
    "channelTitle": "", 
    "liveBroadcastContent": "none" 
    } 
    }, 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/hwBjyzzUi0M3qf0QKR6EvAD6RTs\"", 
    "id": { 
    "kind": "youtube#playlist", 
    "playlistId": "PL2Co6UkQCJCrvFuM-KEY6Pfccm2PKNhGe" 
    }, 
    "snippet": { 
    "publishedAt": "2015-08-03T21:28:26.000Z", 
    "channelId": "UCqHlUWMIi07M5U6kVSJT7RA", 
    "title": "Popular Videos - OpenCourseWare & Teacher", 
    "description": "", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/SQI2I48QUBQ/default.jpg" 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/SQI2I48QUBQ/mqdefault.jpg" 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/SQI2I48QUBQ/hqdefault.jpg" 
    } 
    }, 
    "channelTitle": "", 
    "liveBroadcastContent": "none" 
    } 
    }, 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/igp-ZANLFmAQxfuTsPHpNeJwdwg\"", 
    "id": { 
    "kind": "youtube#playlist", 
    "playlistId": "PLfu3FYNDydnC8bAZrkXpFXXxbCE8O9ZYS" 
    }, 
    "snippet": { 
    "publishedAt": "2015-07-07T15:38:00.000Z", 
    "channelId": "UCqL4K9FRJ90uHEvRsSXi5cA", 
    "title": "Popular Videos - MIT OpenCourseWare & Field", 
    "description": "", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/PgE9oBDIgdc/default.jpg" 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/PgE9oBDIgdc/mqdefault.jpg" 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/PgE9oBDIgdc/hqdefault.jpg" 
    } 
    }, 
    "channelTitle": "", 
    "liveBroadcastContent": "none" 
    } 
    }, 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"sGDdEsjSJ_SnACpEvVQ6MtTzkrI/9G1cGWzCiMxuc7L_KpgWTyJFjzk\"", 
    "id": { 
    "kind": "youtube#playlist", 
    "playlistId": "PL2Co6UkQCJCp_IpNmpim5CPhKC0gE95jv" 
    }, 
    "snippet": { 
    "publishedAt": "2015-06-19T21:28:19.000Z", 
    "channelId": "UCqHlUWMIi07M5U6kVSJT7RA", 
    "title": "Popular Videos - OpenCourseWare & Educational Technology", 
    "description": "", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/ad4oFCHBSRQ/default.jpg" 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/ad4oFCHBSRQ/mqdefault.jpg" 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/ad4oFCHBSRQ/hqdefault.jpg" 
    } 
    }, 
    "channelTitle": "", 
    "liveBroadcastContent": "none" 
    } 
    } 
] 
} 

变化json.data.items到json.items

+0

感谢@Nick Hammond,它的工作原理 –

where is vid在JSON响应中的eo URL?因为我也在搜索它,所以请让我知道YouTube api响应的视频URL在哪里。

+0

您可以从'item.id.videoId'获得视频ID,然后与youtube URL https://www.youtube.com/watch?v=['item连接。 id.videoId'。希望这就是你正在寻找的....“{”“”“”youtube#searchResult“, ”etag“:”\“sGDdEsjSJ_SnACpEvVQ6MtTzkrI/-D3XCQyB-podnhbL-keBQoIZMb8 \”“, ”id“: “kind”:“youtube#video”, “videoId”:“vOpp8nOp9uA” },' –

+0

@AvineshKumar谢谢,我知道有可能以这种方式访问​​视频,但问题在于直接'youtube.com'因此我正在寻找另一种方式,正如我已经搜索过的,有许多网站也在使用YouTube API,但是视频源与链接网址不同......正如我之前在这里问过的,请检查出来[http://*.com/questions/32444021/how-to-get-url-for-video-using-youtube-api-v3] –