Get Youtube Video URL using Youtube V3 API & JavaScript
-
I have searched alot on this topic but i was only able to find resources related to V2 of Youtube API which is nor depricated. What i want to do is get Youtube video title, thumbnail and link from the JSON response of the Youtube V3 API URL using Javascript. I run the code below but get an error message 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> My url is https://www.googleapis.com/youtube/v3/search?part=snippet&order=date&key=AIzaSyC3TJRI1C0EVEdl7b19InH1gv9yrAOX2YU&q=opencourseware this returns { "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" } } ] }
-
Answer:
Change json.data.items to json.items
Avinesh Kumar at Stack Overflow Visit the source
Other answers
where is video URL in json response?as i am also searching for it,so please let me know where is the URL for video in response from YouTube api.
Javiad Murtaza
Related Q & A:
- How to get live stream url from youtube live?Best solution by Stack Overflow
- How to get the previous URL after using window.history.back?Best solution by stackoverflow.com
- How do you get a video you make on Windows Movie Maker onto Youtube?Best solution by wikihow.com
- How to get contacts using address book API in ios programming?Best solution by Stack Overflow
- How to get a Video off your YouTube channel?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.