Html 嵌入 YouTube 视频 - 拒绝在框架中显示,因为它将“X-Frame-Options”设置为“SAMEORIGIN”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25661182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:38:39  来源:igfitidea点击:

Embed YouTube video - Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'

htmldjangovideoyoutubeyoutube-api

提问by doniyor

I am trying to feed my Django page with some resource I am getting from somewhere else.

我正在尝试使用我从其他地方获得的一些资源来提供我的 Django 页面。

Inside the feed, I have YouTube videos with URL like: https://www.youtube.com/watch?v=A6XUVjK9W4o

在提要中,我有 YouTube 视频,其 URL 如下: https://www.youtube.com/watch?v=A6XUVjK9W4o

Once I added this into my page, the video does not show up, saying:

一旦我将此添加到我的页面中,视频就不会显示,说:

Refused to display 'https://www.youtube.com/watch?v=A6XUVjK9W4o' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

拒绝在框架中显示“ https://www.youtube.com/watch?v=A6XUVjK9W4o”,因为它将“X-Frame-Options”设置为“SAMEORIGIN”。

Then I said, well, what if I change watchto embed. Then YouTube player shows up, but no video, saying:

然后我说,好吧,如果我watch改为embed. 然后 YouTube 播放器出现,但没有视频,说:

Enter code here

在此处输入代码

How can I get this to work?

我怎样才能让它发挥作用?

I am showing it in HTML like this:

我在 HTML 中显示它是这样的:

<iframe width="420" height="315"
    src="{{vid.yt_url}}">
</iframe>

I googled almost for an hour, but no sign of success. I tried to append &output=embed- nada...

我用谷歌搜索了将近一个小时,但没有成功的迹象。我试图追加&output=embed- nada ...

回答by Nyi Ma Lay Win Lae Aye

You must ensure the URL contains embedrather watchas the /embedendpoint allows outside requests, whereas the /watchendpoint does not.

您必须确保 URL 包含embed而不是watch因为/embed端点允许外部请求,而/watch端点不允许。

<iframe width="420" height="315" src="https://www.youtube.com/embed/A6XUVjK9W4o" frameborder="0" allowfullscreen></iframe>

回答by d1jhoni1b

The YouTube URL in srcmust have and use the embedendpoint instead of watch, so for instance let's say you want to embed this YouTube video: https://www.youtube.com/watch?v=P6N9782MzFQ(browser's URL).

中的 YouTube URLsrc必须具有并使用embed端点而不是watch,例如,假设您要嵌入此 YouTube 视频:(https://www.youtube.com/watch?v=P6N9782MzFQ浏览器的 URL)。

You should use the embedendpoint, so the URL now should be something like https://www.youtube.com/embed/P6N9782MzFQ. Use this value as the URL in the srcattribute inside the iframe tag in your HTML code, for example:

您应该使用embed端点,因此 URL 现在应该类似于https://www.youtube.com/embed/P6N9782MzFQ. 将此值用作srcHTML 代码中 iframe 标记内属性中的 URL ,例如:

<iframe width="853" height="480" src="https://www.youtube.com/embed/P6N9782MzFQ" frameborder="0" allowfullscreen ng-show="showvideo"></iframe>

<iframe width="853" height="480" src="https://www.youtube.com/embed/P6N9782MzFQ" frameborder="0" allowfullscreen ng-show="showvideo"></iframe>

So just replace https://www.youtube.com/watch?v=with https://www.youtube.com/embed/and of course check for your video's ID. In this sample, my video ID is P6N9782MzFQ.

因此,只需更换https://www.youtube.com/watch?v=https://www.youtube.com/embed/您的视频ID的过程检查和。在此示例中,我的视频 ID 是P6N9782MzFQ.

回答by Popa Andrei

You only need to copy <iframe> from the YouTube Embed section (click on SHARE below the video and then EMBED and copy the entire iframe).

您只需要从 YouTube 嵌入部分复制 <iframe>(点击视频下方的分享,然后嵌入并复制整个 iframe)。

回答by rii

If embed no longer works for you, try with /vinstead:

如果 embed 不再适合您,请尝试使用/v

<iframe width="420" height="315" src="https://www.youtube.com/v/A6XUVjK9W4o" frameborder="0" allowfullscreen></iframe>

回答by Raunaq Kochar

Along with the embed, I also had to install the Google Cast extension in my browser.

除了嵌入,我还必须在浏览器中安装 Google Cast 扩展程序。

<iframe width="1280" height="720" src="https://www.youtube.com/embed/4u856utdR94" frameborder="0" allowfullscreen></iframe>