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

17,797

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>
Share:
17,797
Lakshmi Deepak Kandru
Author by

Lakshmi Deepak Kandru

Updated on June 05, 2022

Comments