我试图在Asp.net mvc4 web应用程序中通过频道名称检索youtube视频。我试着用谷歌搜索并测试了我发现的所有代码,但它最终显示"请求执行失败:http://gdata.youtube.com/feeds/api/users/channelname/uploads?orderby=published"任何帮助将不胜感激,我仍然试图谷歌解决方案。由于
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);
}
V2 YouTube API已弃用。您应该使用V3。如果你想获得一个频道上传的视频,你必须提供频道名或频道ID,获取上传的播放列表,然后从该播放列表中获取视频。请在这里查看代码示例。或者,您可以通过指定频道ID并设置type=video
来搜索给定频道的视频。