无法嵌入某些 YouTube 视频,iframe 显示 "the video contains content from WMG. it is restricted from playback on certain sites"

Can't embed certain YouTube videos, iframe displays "the video contains content from WMG. it is restricted from playback on certain sites"

我正在使用 ClojureScript 和 Reagent 构建一个即时 YouTube 客户端,它目前在 http://instatube.net/ when I try to play most music videos, it displays the error "the video contains content from X. it is restricted from playback on certain sites", yet the same video plays just fine on my local server, the iframe sends the same referer and origin headers in both cases, it also works when I embed the video here https://www.w3schools.com/html/tryit.asp?filename=tryhtml_default 上线 我不明白这是什么问题,这是我的 iframe 试剂组件

[:iframe {:class "embed-responsive-item"
          :allow-full-screen "allowfullscreen"
          :frame-border 0
          :auto-play 1
          :src (str "https://www.youtube.com/embed/" (if (nil? videoId) 
          "SW-BU6keEUw" videoId) "?autoplay=1&enablejsapi=1")}]]

这是我发送到 YouTube 搜索 API v3 的 ajax 请求 cljs-ajax

(fn [term]
  (ajax/GET
    "https://www.googleapis.com/youtube/v3/search"
    {:params {:q term
              :maxResults 5
              :part "snippet"
              :type "video,playlist"
              :key YOUTUBE_API_KEY}
    :handler handle-youtube-resonse
    :response-format (ajax/json-response-format {:keywords? true})
    :headers {:referer "https://www.youtube.com/"
              :x-spf-referer "https://www.youtube.com/"}))

这个SO post也遇到了相关的错误。

Certain videos have a domain-level whitelist or blacklist applied to them. This is done at the discretion of the content owner.

If there is a whitelist or a blacklist, and the domain of the embedding site can't be determined (perhaps because of there not being a real referring domain in the case of your native application), then the default behavior is to block playback.

This blog post has a bit more detail as well: http://youtube-eng.blogspot.co.uk/2011/12/understanding-playback-restrictions_28.html

另一个答案也有一点,如果您尝试从应用程序而不是网页请求列入黑名单的视频,您将收到以下错误消息:

"This video contains content from ___. It is restricted from playback on certain sites."

您被错误地列入了黑名单。检查 this answer in a SO post 以解决此问题。

You need to supply your Youtube API request with an origin.

In your request header for the Youtube video, set Referer to the domain in which you intend to be making the call from, (for ex. the domain of your app's corresponding website). If you don't have a domain, you could easily write some other domain, and that might work too.

  • For Android (Java), you can see an example .
  • For iOS, look above
  • For React Native, you can use the origin prop on the component to your domain (origin is mentioned in the docs but doesn't tell you much about it).
  • Here is another example of the same issue in a browser when an extension blocked the Referer header from being sent for good measure.

Note:This answer works for the V3 API of Youtube.