0
Good afternoon people, I’m trying to make an integration with the Youtube API, where the videos can be played by an iframe on my page, which by the way is in ASP.NET. The documentation gave me this code.
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '360',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
So far so good, I managed to use it correctly, but I would like to know how I do in case of PRIVATE videos ? Is there a way to send an authentication to the API that identifies me as the account holder where the video was uploaded ? Thank you!
I have used the API with private videos without having to send any header and it worked normally. There was some error in the console?
– Davis Roberto
No, just the message on the frame itself that the video cannot play because it is not available.
– Gabriel Bastos
Works with public video?
– Davis Roberto
Yes, with public videos it works perfectly.
– Gabriel Bastos
Dear Gabriel, only with
unlisted
you will get, private no.– Guilherme Nascimento
Ahh it is true, in this way it will only be possible to watch the video if it is referenced directly at the url. It will suit me! Thank you William ! Posted as an answer I mark.
– Gabriel Bastos