使用c#从我的YouTube视频频道搜索上传的YouTube视频

时间:2015-10-13 11:49:44

标签: c# video youtube youtube-channels

在Youtube中,请帮助我搜索我在youtube频道上传的视频。 这是我正在尝试的代码..

namespace Google.Apis.YouTube.Samples
{

that you have enabled the YouTube Data API for your project.
  /// </summary>
  internal class Search
  {
    [STAThread]
    static void Main(string[] args)
    {
      Console.WriteLine("YouTube Data API: Search");
      Console.WriteLine("========================");

      try
      {
        new Search().Run().Wait();
      }
      catch (AggregateException ex)
      {
        foreach (var e in ex.InnerExceptions)
        {
          Console.WriteLine("Error: " + e.Message);
        }
      }

      Console.WriteLine("Press any key to continue...");
      Console.ReadKey();
    }

    private async Task Run()
    {
      var youtubeService = new YouTubeService(new BaseClientService.Initializer()
      {
          ApiKey = "API KEY ",
          ApplicationName = "YouTubeUpload"
      });

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

在运行上述代码时,我收到“错误请求400”异常Exception screenshot

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您需要在Google Console Developer

中使用API​​密钥设置字段“ApiKey”

首先,为项目启用Youtube Data API,然后创建凭据以获取APIKey。

相关问题