I’m looking to add YouTube videos to my web app, but I want to prevent users from accessing the direct links to the videos while they’re using my app. How can I achieve this?
To be honest I don’t have any idea on how to do this. So please help guys!
2
Answers
To embed YouTube videos in your web app while safeguarding the direct video links, you can use the YouTube Player API provided by YouTube. This allows you to embed YouTube videos on your website without exposing the direct video links.
Here’s a general approach to achieve this using the YouTube Player API:
Include the YouTube Player API script: First, include the YouTube Player API script in your HTML. You can do this by adding the following script tag to your HTML file:
<script src="https://www.youtube.com/iframe_api"></script>
Create a placeholder for the video: Create a placeholder element (e.g., a div) where you want to embed the YouTube video.
<div id="player"></div>
Initialize the YouTube player: In your JavaScript code, initialize the YouTube player by specifying the video ID and other options. This code should be executed after the YouTube Player API script has loaded.
@Otabek has a solid answer for putting the logic in JavaScript. The only issue is that any user can view the JavaScript in DevTools to get the link.
The best option you have is to only allow the video to be embedded on specific domains.
This will make it so you have to be on your site to view it.
Your application has to make a request for the video, so there is no way to truly hide the link.