2015-10-04 76 views
0

我試圖通過頻道名稱在Asp.net mvc4 web應用程序中檢索youtube視頻。我嘗試了使用Google搜索的所有代碼進行搜索並進行了測試,但最終結果爲「執行請求失敗:http://gdata.youtube.com/feeds/api/users/channelname/uploads?orderby=published「 。任何幫助將被讚賞,我仍然試圖谷歌它的解決方案。 感謝在Asp.net MVC4中通過頻道名稱檢索youtube視頻時出錯

型號

public class Youtube 
    { 
     public string YouTubeMovieID { get; set; } 
     public string YouTubeMovieTitle { get; set; } 
    } 

    public class YouTubeHelper 
    { 
     const string YT_CHANNEL_NAME = "Heaven Luk"; 
     const string YT_DEVELOPER_ID = "Devloper ID here"; 

     public static List<Youtube> GetVideos() 
     { 
      YouTubeRequestSettings ytSettings = new YouTubeRequestSettings("MyTestingWebsite", YT_DEVELOPER_ID); 
      YouTubeRequest ytRequest = new YouTubeRequest(ytSettings); 
      string feedURL = String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads?orderby=published", YT_CHANNEL_NAME); 
      Feed<Video> videoFeed = ytRequest.Get<Video>(new Uri(feedURL)); 

      return (from video in videoFeed.Entries 
        select new Youtube() { YouTubeMovieID = video.VideoId, YouTubeMovieTitle = video.Title }).ToList(); 
     } 

查看

@model IEnumerable<MyTestingProject.Models.YouTubeHelper> 
@{ 

    Layout = "~/Views/Shared/_Foundation.cshtml"; 
} 

@foreach (var v in Model) 
{ 
<object width="427" height="258"> 
      <param name="movie" value="http://www.youtube.com/v/@v.YouTubeMovieID" /> 
      <param name="allowFullScreen" value="true" /> 
      <param name="allowscriptaccess" value="always"/> 
      <param name="wmode" value="opaque" /> 
      <embed src="http://www.youtube.com/v/@v.YouTubeMovieID" 
       type="application/x-shockwave-flash" width="427" 
       height="258" allowscriptaccess="always" allowfullscreen="true" 
       wmode="opaque"></embed> </object> 

} 

控制器

public ActionResult Videos() 
     { 
      var videos = YouTubeHelper.GetVideos(); 
      return View(videos); 
     } 

回答

0

的V2的YouTube API已被棄用。你應該用V3代替。如果您想獲取頻道上傳的視頻,則必須提供頻道名稱或ID,獲取上傳播放列表,然後從該播放列表中獲取視頻。請參閱代碼示例here。或者,您可以通過指定頻道ID並設置type=video來針對給定頻道的視頻search

相關問題