youtubeyoutube-apiyoutube-javascript-apiyoutube-data-api

How to get data of a youtube playlist in JSON format using JavaScript API V3


Youtube stopped support to V2 I'm using till now, can anyone tell me how to get data of a youtube playlist in JSON format using JavaScript API V3

V2 URL I used is as given below http://gdata.youtube.com/feeds/api/playlists/{PLAYLIST_ID}?alt=json-in-script&callback=showMyVideos

Resolved

v3 URL is https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={PLAYLIST_ID}&key={KEY}


Solution

  • There are a number of sample applications for the Javascript v3 client library that can be found here:

    https://developers.google.com/youtube/v3/code_samples/javascript

    The call you are probably after is PlaylistItems:list and can be seen here:

    https://developers.google.com/youtube/v3/docs/playlistItems/list

    and can be called using:

    GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&id=YOUR-PLAYLIST-ID&key={YOUR_API_KEY}
    

    Something along these lines will get details of the first 50 records in a specified playlist using the Javascript client library. The result is stored in the response object.

    <!doctype html>
    <html>
    
    <head>
        <title>YouTube</title>
    </head>
    
    <body>
    
        <script>
            function onGoogleLoad() {
                gapi.client.setApiKey('{YOUR-API-KEY}');
                gapi.client.load('youtube', 'v3', function() {
    
                    var request = gapi.client.youtube.playlistItems.list({
                        part: 'snippet',
                        playlistId: '{PLAYLIST-ID-HERE}',
                        maxResults: 50
                    });
    
                    request.execute(function(response) {
                        for (var i = 0; i < response.items.length; i++) {
                            console.log(response.items[i].snippet.title + " published at " + response.items[i].snippet.publishedAt)
                        }
                    });
                });
            }
        </script>
    
        <script src="https://apis.google.com/js/client.js?onload=onGoogleLoad"></script>
    
    </body>
    
    </html>