2012-09-29 82 views
2

我正在嘗試使用YouTube API v2.0 - Retrieving Live Events 來吸引最近的YouTube直播活動Feed但我的腳本似乎並不奏效。這是我的代碼。嵌入YouTube Live Feed

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    </head> 
    <body> 

    <div id="static_video"></div> 

     <script> 
      $(document).ready(function() { 
      $(function() { 
       $.getJSON("https://gdata.youtube.com/feeds/api/users/ElektraRecords/live/events?v=2&inline=true&alt=json-in-script&status=pending&orderby=published$callback=?", 
      function (data) { 
       $.each(data.feed.content.entry, function(i,entry) { 
        var videoid = 'http://www.youtube.com/embed/' + entry.yt$videoid.$t; 
        var title = '<h1>' + entry.media$title.$t + '</h1>'; 
       var video = +title+ "<iframe width='420' height='315' src='"+videoid+ "' frameborder='0' allowfullscreen></iframe>"; 
       $('#static_video').html(video); 
        }); 
       }); 
      }); 
     }); 
     </script> 

    </body> 
</html> 
+0

這與v3的API改變。 Dupe在這裏:[我可以使用YouTube的LiveStreaming API訪問所有現場直播?](http://stackoverflow.com/questions/33380083/can-i-access-all-live-broadcasts-with-youtubes-livestreaming-api) – JAL

回答

0

您缺少依賴腳本。顯然,您需要在文檔的標題中包含API的gdata部分。請參閱您的瀏覽器的調試器,你的腳本,我穿上的jsfiddle:http://jsfiddle.net/2NDrC/ - 你可能希望嵌入API是在這裏:https://code.google.com/p/google-api-javascript-client/

如果你看一下你正在使用JavaScript檢索URL呢,你看,這是方法調用的方法的API GDATA內:

URL:https://gdata.youtube.com/feeds/api/users/ElektraRecords/live/events?v=2&inline=true&alt=json-in-script&status=pending&orderby=published$callback=?

返回

gdata.io.handleScriptLoaded({"version":"1.0","encoding":"UTF-8","feed":{"xmlns$app":"http://www.w3...

並作爲JavaScript執行。因此必須提供一個名爲gdata.io.handleScriptLoaded的方法才能使執行成功。

一種解決方法,解釋如何假一gdata.io.handledScriptLoaded方法只提取數據顯示如下:https://groups.google.com/forum/?fromgroups=#!topic/jquery-en/83Kpr9oSxFM