当前位置:  开发笔记 > 编程语言 > 正文

从频道获取所有视频 - Youtube API v3 c#

如何解决《从频道获取所有视频-YoutubeAPIv3c#》经验,为你挑选了1个好方法。

是否可以从某个频道获取所有视频(不是我的)?如果可能,我可以使用简单的API密钥,还是应该使用OAuth 2.0凭据?



1> 小智..:

我这样做了,它对我有用,我使用了Nuget Packet manager的Youtube API v3

using Google.Apis.Services;
using Google.Apis.YouTube.v3;

public ActionResult GetVideo(YouTubeData objYouTubeData)
{
    try
    {
        var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "Your API Key" });
        var channelsListRequest = yt.Channels.List("contentDetails");
        channelsListRequest.ForUsername = "kkrofficial";
        var channelsListResponse = channelsListRequest.Execute();
        foreach (var channel in channelsListResponse.Items)
        {
            // of videos uploaded to the authenticated user's channel.
            var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
            var nextPageToken = "";
            while (nextPageToken != null)
            {
                var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
                playlistItemsListRequest.PlaylistId = uploadsListId;
                playlistItemsListRequest.MaxResults = 50;
                playlistItemsListRequest.PageToken = nextPageToken;
                // Retrieve the list of videos uploaded to the authenticated user's channel.
                var playlistItemsListResponse = playlistItemsListRequest.Execute();
                foreach (var playlistItem in playlistItemsListResponse.Items)
                {
                    // Print information about each video.
                    //Console.WriteLine("Video Title= {0}, Video ID ={1}", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
                    var qry = (from s in ObjEdbContext.ObjTubeDatas where s.Title == playlistItem.Snippet.Title select s).FirstOrDefault();
                    if (qry == null)
                    {
                        objYouTubeData.VideoId = "https://www.youtube.com/embed/" + playlistItem.Snippet.ResourceId.VideoId;
                        objYouTubeData.Title = playlistItem.Snippet.Title;
                        objYouTubeData.Descriptions = playlistItem.Snippet.Description;
                        objYouTubeData.ImageUrl = playlistItem.Snippet.Thumbnails.High.Url;
                        objYouTubeData.IsValid = true;
                        ObjEdbContext.ObjTubeDatas.Add(objYouTubeData);
                        ObjEdbContext.SaveChanges();
                        ModelState.Clear();

                    }
                }
                nextPageToken = playlistItemsListResponse.NextPageToken;
            }
        }
    }
    catch (Exception e)
    {
        ViewBag.ErrorMessage = "Some exception occured" + e;
        return RedirectToAction("GetYouTube");
    }

    return RedirectToAction("GetYouTube");
}

在此行中提供您的频道名称

channelsListRequest.ForUsername = "kkrofficial"; //kkrofficial is kkr channel name.

请访问此链接 https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有