Hi friends am trying to fetch all the results using youtube api v3 search. Here is the code..
<!doctype html>
<html>
<head>
<title>Search</title>
</head>
<body>
<div id="buttons">
<label> <input id="query" value='cats' type="text"/><button id="search-button" onclick="search()">Search</button></label>
</div>
<div id="search-container">
</div>
<button id="prev-button" class="paging-button" onclick="previousPage(document.getElementById('query').value);">Previous Page</button>
<button id="next-button" class="paging-button" onclick="nextPage(document.getElementById('query').value);">Next Page</button><br><br>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="search.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
Here is the script code..
var search,nextPageToken, prevPageToken;
function search(pageToken) {
var q = $('#query').val();
gapi.client.load('youtube', 'v3', function() {
gapi.client.setApiKey('Api-key');
var request = gapi.client.youtube.search.list({
q: q,
part: 'snippet',
type: 'playlist',
maxResults: 50,
order: 'viewCount'
});
if (pageToken) {
request.pageToken = pageToken;
}
request.execute(function(response) {
nextPageToken = response.result.nextPageToken;
var nextVis = nextPageToken ? 'visible' : 'hidden';
$('#next-button').css('visibility', nextVis);
prevPageToken = response.result.prevPageToken
var prevVis = prevPageToken ? 'visible' : 'hidden';
$('#prev-button').css('visibility', prevVis);
var playlistItems = response.result.items;
if (playlistItems) {
$.each(playlistItems, function(index, item) {
displayResult(item.id);
});
} else {
$('#search-container').html('Sorry you have no uploaded videos');
}
});
});
}
function displayResult(videoSnippet) {
var title = videoSnippet.playlistId;
$('#search-container').append( title + '<br>');
}
function nextPage(val) {;
var search_val=val;
search(nextPageToken,search_val);
}
// Retrieve the previous page of videos in the playlist.
function previousPage(listid) {
var search=listid;
search(prevPageToken,search);
}
Am trying to fetch the all results using pagetokens but am unable to get all the results Am getting the same 50 results repeatedly..How can I get the exact results using search
To use the
pageToken
parameter, you should place here the string code that you get in your first set of results.For example, I use Search: list with these parameters:
Here are the results
Now if we use
maxResults: 3
in this request, we get the first 3 as expected.You will notice in the above results the parameter "nextPageToken": "CAMQAA", this is the string code that you need to supply on your next request to get the next page, or in this example to get the next three results which are:
Then use again the
"nextPageToken": string
to get the next page results.Hope this information helps you.