2016-07-15 180 views

回答

0

YouTube數據API,您可以請求一組的匹配特定搜索詞的視頻。該API支持各種標準的Google數據查詢參數以及與視頻搜索特別相關的自定義參數。

要執行搜索請求,您需要創建一個YouTubeQuery對象來指定您的搜索條件並將該對象傳遞給YouTubeRequest.Get()方法。

在客戶端庫代碼中,Query類和YouTubeQuery等子類負責構建供稿網址。在上面的例子中的YouTubeQuery對象構造以下供稿網址:

這裏是它採用.NET平臺上的示例代碼段:

var searchListRequest = youtubeService.Search.List("snippet"); 
     searchListRequest.Q = "Google"; // Replace with your search term. 
     searchListRequest.MaxResults = 50; 

     // Call the search.list method to retrieve results matching the specified query term. 
     var searchListResponse = await searchListRequest.ExecuteAsync(); 

     List<string> videos = new List<string>(); 
     List<string> channels = new List<string>(); 
     List<string> playlists = new List<string>(); 

     // Add each result to the appropriate list, and then display the lists of 
     // matching videos, channels, and playlists. 
     foreach (var searchResult in searchListResponse.Items) 
     { 
     switch (searchResult.Id.Kind) 
     { 
      case "youtube#video": 
      videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId)); 
      break; 

      case "youtube#channel": 
      channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId)); 
      break; 

      case "youtube#playlist": 
      playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId)); 
      break; 
     } 
     } 

有關如何檢索搜索結果的完整代碼,檢查:https://developers.google.com/youtube/v3/code_samples/dotnet#search_by_keyword

+0

我試過這個http://pastebin.com/CmcmtnhM,但我得到了2個錯誤「搜索」不是「Google.GData.YouTube.YouTubeService」的成員,而「類型'var'未定義。」 – Ranieri