2012-07-26 28 views
1

我正在使用YouTube Data API for .NET使用YouTube Api 2.0的相關視頻限制

我呼籲YouTubeRequest classGetRelatedVideos function並返回25個視頻,這都涉及到視頻,就像這樣:

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); 

這裏是請求鏈接:

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

但我得到這個錯誤

'max-results'para儀表不支持此資源

如果我只是用:

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

然後我得到的25個視頻。但我想獲得50個視頻和更多的頁面。我能夠得到的結果爲以下網址:

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

在這裏,我得到迴應,但但我只得到25個視頻,即使我通過50爲max-results參數。

如何獲得特定視頻的50個相關視頻,而不是默認的25(50是max-results的最大值)。

+0

轉到https://gdata.youtube.com/feeds/api/videos/1FJHYqE0RDg/related?max-results=50&start-index=1我在瀏覽器中實際得到50個結果。當你在瀏覽器中下載文件時,你會得到50個結果嗎? – casperOne 2012-07-26 14:36:07

+0

@casperOne是的我也得到了50個結果,但在代碼中,relatedVideos有25個條目..我不知道爲什麼..並且感謝您的關注 – unbalanced 2012-07-26 14:38:36

回答

1

您不應該自己創建URL字符串,而應該使用YouTubeRequest class上的屬性爲您設置它們。

例如,獲得Video實例時,你希望在YouTubeRequestSettings實例指定PageSize property,就像這樣:

// 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")); 

但是,要使用一個不同的YouTubeRequestSettings當撥打GetRelatedVideos method時,附加到YouTubeRequest實例:

// 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); 

現在它將返回50個視頻。如果您在獲取視頻時嘗試設置PageSize媒體資源,則會收到錯誤消息,因爲獲取單個視頻時max-results參數無效。

然後你可以寫出來的條目的數量來驗證50返回:

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

結果將是:

50在第一頁相關視頻。

+0

謝謝你的回答。我解決了我自己的程序,就像你提到的那樣.. Feed