Limit of Related Videos with Youtube Api 2.0

I'm using the YouTube Data API for .NET.

I am calling the GetRelatedVideos function on the YouTubeRequest class and it returns 25 videos that are related to a video, like so:

Video video = Request.Retrieve<Video>(
    new Uri(String.Format("https://gdata.youtube.com/feeds/api/videos/{0}{1}",
        vID ,"?max-results=50&start-index=1")));  

Feed<Video> relatedVideos = Request.GetRelatedVideos(video);

return FillVideoInfo(relatedVideos.Entries);

Here is the request link:

https://gdata.youtube.com/feeds/api/videos/1FJHYqE0RDg?max-results=50&start-index=1

But I get this error

The 'max-results' parameter is not supported on this resource

If I just use:

https://gdata.youtube.com/feeds/api/videos/1FJHYqE0RDg

Then I get 25 videos. But I want to get 50 videos and and page for more. I am able to get a result for the following URL:

https://gdata.youtube.com/feeds/api/videos/1FJHYqE0RDg/related?max-results=50&start-index=1

Here I get a response, but but I only get 25 videos even though I passed 50 for the max-results parameters.

How can I get 50 related videos for a particular video at a time instead of the default 25 (50 is the max for max-results ).


Instead of creating the URL string yourself, you should be using the properties on the YouTubeRequest class to set them for you.

For example, when getting the Video instance, you don't want to specify the PageSize property on the YouTubeRequestSettings instance, like so:

// Create the request.
var request = new YouTubeRequest(
    new YouTubeRequestSettings("my app", null) { AutoPaging = false });

// Get the video.
var video = request.Retrieve<Video>(
    new Uri("https://gdata.youtube.com/feeds/api/videos/1FJHYqE0RDg"));

However, you want to use a different YouTubeRequestSettings attached to the YouTubeRequest instance when making the call to GetRelatedVideos method:

// Create the request again.  Set the page size.
request = new YouTubeRequest(
    new YouTubeRequestSettings("my app", null) { 
        AutoPaging = false, PageSize = 50
 });

 // Get the related videos.
 var related = request.GetRelatedVideos(video);

Now it will return 50 videos. If you try and set the PageSize property when getting the video, you get an error because the max-results parameter is invalid when getting a single video.

You can then write out the count of the entries to validate that 50 are returned:

// Write out how many videos there are.
Console.WriteLine(string.Format(CultureInfo.CurrentCulture, 
    "{0} related videos in first page.", related.Entries.Count()));

The result will be:

50 related videos in first page.

链接地址: http://www.djcxy.com/p/28988.html

上一篇: Youtube API获取视频查询

下一篇: 与YouTube Api 2.0相关的视频限制