· 6 years ago · Oct 24, 2019, 03:20 PM
1<!DOCTYPE html>
2<html>
3<head>
4<title>YouTube JSON Example</title>
5
6<script type="text/javascript">
7 const api = undefined; // Specify your api key here
8
9 // Load API library
10 function onLoad() {
11 if(api) {
12 gapi.client.load('youtube', 'v3', loadYouTubeApi);
13 } else {
14 document.getElementById('output').innerHTML += 'API key has not been specified!';
15 }
16 }
17
18 // Set API Key
19 function loadYouTubeApi() {
20 gapi.client.setApiKey(api);
21 search('sitepoint');
22 }
23
24 // Call the search.list()
25 function search(queryTerm) {
26 let request = gapi.client.youtube.search.list({
27 part: 'id',
28 q: queryTerm
29 });
30 // Execute the request call and output it in HTML view
31 request.execute((response) => {
32 const responseString = JSON.stringify(response, '', 2);
33 document.getElementById('output').innerHTML += responseString;
34 })
35 }
36</script>
37<script src="https://apis.google.com/js/client.js?onload=onLoad" type="text/javascript"></script>
38</head>
39<body>
40<h1>YouTube Search Results</h1>
41<pre id="output"></pre>
42</body>
43</html>