如何从c#中获取API的YouTube统计信息

时间:2015-07-14 22:38:31

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

如何获取YouTube视频统计信息?例如:来自api的喜欢,评论计数,查看计数等。

以下代码有效,但返回的值不包括统计信息。

根据文件,我应该可以做到:

var searchListRequest = youtubeService.Search.List("id,snippet,statistics");

但这会返回错误。

我得到的错误是:

  

Google.Apis.Requests.RequestError statistics [400]   错误[消息[统计]位置[part -parameter]   原因[unknownPart]域[youtube.part]]

代码来自here

/*
*/   
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace Google.Apis.YouTube.Samples
{
  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 = "REPLACE_ME",
        ApplicationName = this.GetType().ToString()
      });

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

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

2 个答案:

答案 0 :(得分:1)

确保您使用的是有效的API密钥,并且已为项目启用了YouTube Data API。另外,请仔细检查您是否使用了.NET client library的最新版本。

答案 1 :(得分:0)

我使用了您在问题中给出的代码,并通过提供API密钥运行它并成功运行。希望你的密钥或应用程序设置有问题。您是否在应用程序设置中允许使用Youtube和Google API。请确保那件事