从Youtube频道搜索视频,该视频仅根据视频名称在​​该频道上传

时间:2015-10-15 05:46:10

标签: c# youtube-api youtube-data-api google-api-dotnet-client youtube-channels

我正在尝试仅根据特定视频的视频名称搜索我频道中的视频。但是,当我尝试使用以下代码时,我会看到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 个答案:

答案 0 :(得分:1)

根据documentation,您可以在搜索请求中使用'channelId'参数。

searchListRequest.ChannelId = {YOUR_CHANNEL_ID};

(我不是c#人。请检查属性名称)