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
Embed YouTube video - Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
提问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 watch
to embed
. Then YouTube player shows up, but no video, saying:
然后我说,好吧,如果我watch
改为embed
. 然后 YouTube 播放器出现,但没有视频,说:
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 /embed
endpoint allows outside requests, whereas the /watch
endpoint 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 src
must have and use the embed
endpoint 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 embed
endpoint, so the URL now should be something like https://www.youtube.com/embed/P6N9782MzFQ
. Use this value as the URL in the src
attribute inside the iframe tag in your HTML code, for example:
您应该使用embed
端点,因此 URL 现在应该类似于https://www.youtube.com/embed/P6N9782MzFQ
. 将此值用作src
HTML 代码中 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 /v
instead:
如果 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>