我在我的.net项目api youtube中实现了。
这是我的代码
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "MY_API_KEY",
ApplicationName = "MY_APPLICATION_NAME"
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = SearchText;
searchListRequest.MaxResults = 50;
searchListRequest.Order = SearchResource.ListRequest.OrderEnum.ViewCount;
var searchListResponse = await searchListRequest.ExecuteAsync();
foreach (var searchResult in searchListResponse.Items)
{
if (searchResult.Id.Kind == "youtube#video")
{
}
}
searchResult中没有STATISTICS(例如视图计数)。如何?
因为search.list
没有statitics
部分,所以需要调用两次API。
- 一次请求
search.list
- 你得到了频道的id
- 以及与
channel.list
的第二次呼叫,具有信道id和parameter: statistics
那么你有viewCount
Doc可以帮助:https://developers.google.com/youtube/v3/docs/search/listhttps://developers.google.com/youtube/v3/docs/channels/list
我在使用他们基于Javascript的API搜索功能时遇到了同样的问题。
看起来他们没有基于搜索的API的内置"视图"选项。https://developers.google.com/youtube/v3/docs/search#snippet
无论如何,您都可以使用他们基于JSON的API并创建基于AJAX的搜索框,该搜索框返回基于JSON的响应,并提供视图计数选项!https://developers.google.com/youtube/2.0/developers_guide_jsonc
这是我自己创建的,请查看:
$(document).ready(function){
var q = $('#query');
$('#search-button').click(function(e) {
var url = "https://gdata.youtube.com/feeds/api/videos?q=" + q.val() + "&v=2&alt=jsonc";
$.getJSON( url, function( response ) {
for(var i = 0; i < response.data.items.length; i++) {
var tr = "<tr>",
title = "<td>" + response.data.items[i].title + "</td>",
views = "<td>" + response.data.items[i].viewCount + "</td>",
likes = "<td>" + response.data.items[i].likeCount + "</td>",
dislikes = "<td>" + (response.data.items[i].ratingCount - response.data.items[i].likeCount) + "</td>",
endtr = "</tr>";
$('#search-container').append(tr + title + views + likes + dislikes + endtr);
}
});
e.preventDefault();
});
});
http://jsfiddle.net/19m9tLo3/1/