How to Search for YouTube Content?


Here’s a sample code snippet that illustrates a simple approach to search for YouTube content. This example searches for YouTube Videos that is related to Gangnam Style.

The example code has two parts (a) HTML – Search.html (b) JAVASCRIPT – Search.js. Check them out.

(a) Search.html that invokes apis.google.com/js/client.js?onload=onClientLoad

(b) Search.js
// Your use of the YouTube API must comply with the Terms of Service:
// https://developers.google.com/youtube/terms

// Helper function to display JavaScript value on HTML page.
function showResponse(response) {
var responseString = JSON.stringify(response, ”, 2);
document.getElementById(‘response’).innerHTML += responseString;
}

// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load(‘youtube’, ‘v3’, onYouTubeApiLoad);
}

// Called automatically when YouTube API interface is loaded (see line 9).
function onYouTubeApiLoad() {
// This API key is intended for use only in this lesson.
// See http://goo.gl/PdPA1 to get a key for your own applications.
gapi.client.setApiKey(‘AIzaSyCR5In4DZaTP6IEZQ0r1JceuvluJRzQNLE’);

search();
}

function search() {
// Use the JavaScript client library to create a search.list() API call.
var request = gapi.client.youtube.search.list({
part: ‘snippet’, q:’gangnamstyle’

});

// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
}

// Called automatically with the response of the YouTube API request.
function onSearchResponse(response) {
showResponse(response);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s