1

我試圖僅基於特定視頻的視頻名稱搜索出現在我的頻道中的視頻。但是,當我嘗試下面的代碼時,我看到了YouTube中與搜索關鍵字相關的所有視頻。我只想抓取我的頻道中存在的視頻。 使用所有必需的最新庫從Youtube頻道搜索僅在該頻道上傳的視頻,具體取決於視頻名稱

代碼是在這裏

private async Task Run() 
    { 
     var youtubeService = new YouTubeService(new BaseClientService.Initializer() 
     { 
     ApiKey = "My API Key ",//"REPLACE_ME", 
     ApplicationName = this.GetType().ToString() 
     }); 

     var searchListRequest = youtubeService.Search.List("snippet"); 
     searchListRequest.Q = "MYVID"; // 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; 
     } 
     } 

     Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos))); 
     Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels))); 
     Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists))); 
    } 
    } 
} 

回答

1

按照documentation,你可以在搜索請求中使用「渠道ID」參數。

searchListRequest.ChannelId = {YOUR_CHANNEL_ID}; 

(我不是一個C#的人。請檢查屬性名)

相關問題