如何获取YouTube视频统计信息?例如:喜欢,评论计数,视图计数等从api。
下面的代码可以工作,但是返回的值不包括统计信息。
根据文档,我应该能够做:
var searchListRequest = youtubeService.Search.List("id,snippet,statistics");
但是返回一个错误。
得到的错误是:
Google.Apis.Requests。RequestError统计[400]错误[消息[统计]位置[part -parameter]原因(unknownPart)域[youtube.part]]
代码来自这里。
/*
*/
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)));
}
}
}
确保您使用的是有效的API密钥,并且您已经为您的项目启用了YouTube Data API。此外,请仔细检查您使用的是最新版本的。net客户端库。
我使用了问题中给出的代码,并通过提供API密钥使其运行,并成功运行。希望您的密钥或应用程序设置有问题。你允许Youtube和谷歌API在你的应用程序设置。