HAYDEX
HAYDEX

Reputation: 123

I need a working example to retrieve a public youtube playlist using youtube Data API 3 for JavaScript

Am looking for a good example on how to use google's youtube Data API 3 for retrieving a public playlist items using JavaScript, I seem to be struggling to find one in google's own website, the Playlist examples seem to be missing something, they just don't work, I think that google should pay more attention to the documentation of their API.

Thanks!

Upvotes: 2

Views: 2439

Answers (2)

HAYDEX
HAYDEX

Reputation: 123

I finally got it working! According to google's documentation we do the following steps:

1- Create a "project" in Google's Developers Console. 2- Enable "youtube data API 3" in Google's Developer's Console. 3- Create an "API Key" in Google's Developers Console(I have included two hosts 'my own local host' and 'http://www.myownwebsite.com/'). 4- Get your public youtube playlist ID (http://www.youtube.com/playlist?list=PLXXXXXXXXXX the id is the alphanumeric string after PL). 4- Then we add the following in the HTML page:

<head>
<script>
        function load() {
            var playListID = "YOUR_PUBLIC_YOUTUBE_PLAYLIST_ID";
            var requestOptions = {
                playlistId: playListID,
                part: 'snippet',
                maxResults: 10
            };
            var apiKey = "YOUR_API_KEY";
            gapi.client.setApiKey(apiKey);
            gapi.client.load('youtube','v3', function () {  var request = gapi.client.youtube.playlistItems.list(requestOptions);
                                                            request.execute(function (data) { console.log (data) });
                                                        });

        }
</script>
<script src="https://apis.google.com/js/client.js?onload=load"></script>
</head>
<body>
</body>

Upvotes: 1

Damien
Damien

Reputation: 4103

Using jQuery :

$(document).ready(function() {

    var playlistId = "your_playlist_id",
        APIKey = "your_api_key",
        baseURL = "https://www.googleapis.com/youtube/v3/";

    $.get(baseURL + "playlistItems?part=snippet&maxResults=50&playlistId=" + playlistId + "&key=" + APIKey, function(data) {
        // Do what you want with the data
    });

});

Upvotes: 5

Related Questions