Answer the question
In order to leave comments, you need to log in
How to get statistics of YouTube channel views on the site?
I'm trying to get the number of views of a youtube channel on a site.
I need to associate an html tag <p id="youtubeRequest">1120</p>
with a request so that instead of a manually entered number, I get the value of the request.
I added this code to my HTML right after this tag:
<script>
function getViews(params) {
$.ajax({
// я заменяю {} на API key и ID здесь
url: "https://www.googleapis.com/youtube/v3/channels?part=statistics&key={MY_API_KEY}&id={MY_CHANELL_ID}",
jsonp: "callback",
dataType: "jsonp",
success: function(response) {
console.log(response);
document.getElementById("youtubeRequest").innerHTML = response; // не знаю как назначить response
},
error: function(response) {
document.getElementById("youtubeRequest").innerHTML = "nope, doesn't work!";
}
});
}
</script>
document.getElementById
with console.log("error") or alert ("error")Answer the question
In order to leave comments, you need to log in
See how to make requests to api
https://developers.google.com/youtube/v3/docs/chan...
Making a request
Get apik
https://developers.google.com/youtube/v3/getting-s...
UPD:
Example with jQuery. In success, process and paste where necessary
function getViews(params) {
$.ajax({
url: "https://www.googleapis.com/youtube/v3/channels?part=statistics&key={YOUR_API_KEY}&id={YOUR_CHANELL_ID}",
jsonp: "callback",
dataType: "jsonp",
success: function( response ) {
console.log( response );
}
});
}
<script>
function getViews(params) {
$.ajax({
// я заменяю {} на API key и ID здесь
url: "https://www.googleapis.com/youtube/v3/channels?part=statistics&key={MY_API_KEY}&id={MY_CHANELL_ID}",
jsonp: "callback",
dataType: "jsonp",
success: function(response) {
console.log(response);
document.getElementById("youtubeRequest").innerHTML = response.items[0].statistics.viewCount; // Брать только количество просмотров
},
error: function(response) {
document.getElementById("youtubeRequest").innerHTML = "nope, doesn't work!";
}
});
}
getViews() //<--Вызвать функцию!!!
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question