Youtube V3 API – Get Top Video From Keyword

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js”></script>

<script type=”text/javascript”>
function onClientLoad() {
gapi.client.load(‘youtube’, ‘v3’, onYouTubeApiLoad);
}

function onYouTubeApiLoad() {
gapi.client.setApiKey(‘[yourAPIKEY]’);

search(‘[whacktus’);
}

function search(query) {
var request = gapi.client.youtube.search.list({
part: ‘id’,
q: query
});

request.execute(function (response) {

/* sort through the list until a video comes up, then set the URL link */

var link = “”;
i = 0;
while (link == “”) {
if (response.items[i].id.videoId)
{ link = “http://www.youtube.com/embed/” + response.items[i].id.videoId + “?HD=1;rel=0;showinfo=0”; }
i++;
}

var video = $(“#iVideo”);
video.attr(‘src’, link);
});

}
</script>

<iframe id=”iVideo” width=”100%” height=”550″ frameborder=”0″ allowfullscreen></iframe>

<script src=”https://apis.google.com/js/client.js?onload=onClientLoad” type=”text/javascript”></script>

About admin