Reputation: 437
I'm trying to make a script work that uses theYoutube API, i put in a keyword, youtube api finds video -> script takes first result and returns VideoID. Now my problem is that the search function doesn't get triggerd when I press my submit button. Does anyone know what could be the cause of this? This is the code;
html
<script src="assets/js/search.js" type="text/javascript"></script>
<script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
<body>
<center>
<h3 class="h3">KJKerstborrel - Muziekrequest</h3>
<div class="input">
<form name="muziek" action="succes/index" method="post">
<input type="text" class="input-xlarge" id="artiest" name="artiest" placeholder="Gewenste artiest" /><br>
<input type="text" class="input-xlarge" id="nummer" name="nummer" placeholder="Gewenst nummer" required/><br>
<button style="width: 200px;" class="btn btn-success" onClick="search()" type="button">Deze wil ik horen!</button><br>
</form>
</div>
</center>
</body>
JS
// Your use of the YouTube API must comply with the Terms of Service:
// https://developers.google.com/youtube/terms
var YT = 'undefined';
// Helper function to display JavaScript value on HTML page.
function showResponse(response) {
YT = response;
document.getElementById('VideoURL').value = YT.items[0].Id.VideoID;
}
// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
search();
}
// 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('AIzaSyD49-XZ2JV7Rws3KDM2T7nA56Jbi-O7djY');
}
function search() {
// Use the JavaScript client library to create a search.list() API call.
var request = gapi.client.youtube.search.list({
part: 'id',
q: document.getElementById("artiest").value + " - " + document.getElementById("nummer").value,
});
// 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);
}
Upvotes: 3
Views: 6074
Reputation: 787
i followed your example and i got the same error but then followed this tutorial and i was able to make a successful call
https://developers.google.com/api-client-library/javascript/samples/samples
Upvotes: 0
Reputation: 1415
I made a few changes in the JS and i added a field in the html to display the video id.
The html file:
<script src="search.js" type="text/javascript"></script>
<script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
<body>
<center>
<h3 class="h3">KJKerstborrel - Muziekrequest</h3>
<div class="input">
<form name="muziek" action="succes/index" method="post">
<input type="text" class="input-xlarge" id="artiest" name="artiest" placeholder="Gewenste artiest" /><br>
<input type="text" class="input-xlarge" id="nummer" name="nummer" placeholder="Gewenst nummer" required/><br>
<button style="width: 200px;" class="btn btn-success" onClick="search()" type="button">Deze wil ik horen!</button><br>
<input type="text" class="input-xlarge" id="VideoURL" name="VideoURL" placeholder="VideoURL"/><br>
</form>
</div>
</center>
</body>
The JS file:
// Your use of the YouTube API must comply with the Terms of Service:
// https://developers.google.com/youtube/terms
var YT = 'undefined';
// Helper function to display JavaScript value on HTML page.
function showResponse(response) {
YT = response;
// changed: namegiving
document.getElementById('VideoURL').value = YT.items[0].id.videoId;
}
// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
//search(); // changed.
}
// 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('AIzaSyD49-XZ2JV7Rws3KDM2T7nA56Jbi-O7djY');
}
function search() {
// Use the JavaScript client library to create a search.list() API call.
var qVar = document.getElementById("artiest").value
+ " - "
+ document.getElementById("nummer").value;
// changed. added: type
var request = gapi.client.youtube.search.list({
type: 'video',
part: 'id',
q: qVar
});
// 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);
}
Upvotes: 1