Play private videos with the Youtube API

Asked

Viewed 795 times

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?

  • No, just the message on the frame itself that the video cannot play because it is not available.

  • Works with public video?

  • Yes, with public videos it works perfectly.

  • Dear Gabriel, only with unlisted you will get, private no.

  • 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.

Show 1 more comment

1 answer

2


With private I believe it is impossible, what you can/should use is the unlisted

To change privacy do the following steps:

  1. Sign in to beta version of Youtube Studio.
  2. In the menu on the left, select Videos.
  3. Place the mouse cursor over the video you want to update. Select the tab
  4. Live to see your live shipments.
  5. Click the pencil icon in Visibility and choose Público, Privado or Não listado (in your case the last)

More details on: https://support.google.com/youtube/answer/157177?hl=pt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.