youtube data api检索视图计数

时间:2014-09-15 11:15:50

标签: youtube-api youtube-data-api

我在我的.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中的

没有统计数据(例如视图计数)。 怎么样?

2 个答案:

答案 0 :(得分:1)

由于search.list没有statitics部分,您需要拨打两次API。

  • 一次请求search.list
  • 您获得了频道的ID
  • 使用channel.list进行第二次通话,其中包含频道的ID和parameter: statistics

然后你有viewCount

Doc可以提供帮助:https://developers.google.com/youtube/v3/docs/search/list https://developers.google.com/youtube/v3/docs/channels/list

答案 1 :(得分:0)

我使用基于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/