youtube api C#video完整元数据

时间:2016-06-28 00:13:58

标签: youtube-data-api

我正在尝试为youtube播放列表中的每个视频获取youtube视频信息。这样我就可以将我的PowerBI dashbaords和其他电子邮件工具连接到一个SQL数据库,其中包含我的所有YouTube视频元数据。我有一个方法可以获取所有的Snippet视频信息。但是当抓住其他部分时它会失败。这个链接清楚地表明内容细节是一个有效的操作,但API给了我400回。我必须非常接近。有什么想法吗?

https://developers.google.com/youtube/v3/docs/videos/list#parameters

    public YouTubeVideo GetVideoInfo(string id)
    {
        var videoRequest = ytservice.Videos.List("snippet");
        var contentRequest = ytservice.Videos.List("contentdetials");
        var itemRequest = ytservice.Videos.List("items");
        var statsRequest = ytservice.Videos.List("Statistics");
        videoRequest.Id = id;
        contentRequest.Id = id;
        itemRequest.Id = id;
        statsRequest.Id = id;
        var response = videoRequest.Execute();
        var contentResponse = contentRequest.Execute();
        var itemResponse = itemRequest.Execute();
        var statsResponse = statsRequest.Execute();
        var video = new YouTubeVideo();
        if (response.Items.Count > 0)
        {
            video.id = videoRequest.Id;
            video.caption = contentResponse.Items[0].ContentDetails.Caption;
            video.title = response.Items[0].Snippet.Title;
            video.description = response.Items[0].Snippet.Description;
            video.publishDate = response.Items[0].Snippet.PublishedAt.Value;
            video.ageGate = itemResponse.Items[0].AgeGating.Restricted;
            video.viewCount = response.Items[0].Statistics.ViewCount;
            video.likeCount = statsResponse.Items[0].Statistics.LikeCount;
            video.dislikeCount = statsResponse.Items[0].Statistics.DislikeCount;
            video.favoriteCount = statsResponse.Items[0].Statistics.FavoriteCount;
            video.commentCount = statsResponse.Items[0].Statistics.CommentCount;
        }
        else
        {
            //Video not found
        }
        return video;
    }

1 个答案:

答案 0 :(得分:1)

我发现我可以用逗号分隔同一个字符串中的所有调用:)

    public YouTubeVideo GetVideoInfo(string id)
    {
        var videoRequest = ytservice.Videos.List("snippet, contentDetails, statistics");
        videoRequest.Id = id;
        var response = videoRequest.Execute();
        var video = new YouTubeVideo();
        if (response.Items.Count > 0)
        {
            video.id = videoRequest.Id;
            video.caption = response.Items[0].ContentDetails.Caption;
            video.title = response.Items[0].Snippet.Title;
            video.description = response.Items[0].Snippet.Description;
            video.publishDate = response.Items[0].Snippet.PublishedAt.Value;
            //video.ageGate = (response.Items[0].AgeGating.Restricted = null) ? null : response.Items[0].AgeGating.Restricted;
            video.viewCount = response.Items[0].Statistics.ViewCount;
            video.likeCount = response.Items[0].Statistics.LikeCount;
            video.dislikeCount = response.Items[0].Statistics.DislikeCount;
            video.favoriteCount = response.Items[0].Statistics.FavoriteCount;
            video.commentCount = response.Items[0].Statistics.CommentCount;
        }
        else
        {
            //Video not found
        }
        return video;
    }