Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从channel-Youtube API v3 c获取所有视频#_C#_.net_Asp.net Mvc_Asp.net Mvc 4_Youtube Api - Fatal编程技术网

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

C# 从channel-Youtube API v3 c获取所有视频#,c#,.net,asp.net-mvc,asp.net-mvc-4,youtube-api,C#,.net,Asp.net Mvc,Asp.net Mvc 4,Youtube Api,是否可以从某个频道(不是我的)获取所有视频? 如果可能的话,我可以使用一个简单的api密钥,还是应该使用OAuth 2.0凭据?我已经用这种方式完成了,它对我很有效 我使用了Nuget数据包管理器中的Youtube API v3 using Google.Apis.Services; using Google.Apis.YouTube.v3; public ActionResult GetVideo(YouTubeData objYouTubeData) { try {

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

我已经用这种方式完成了,它对我很有效 我使用了Nuget数据包管理器中的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.
点击这个链接

您可以使用API密钥查询所有频道的视频(即使不是您的:)

公共任务GetVideosFromChannelAsync(字符串ytChannelId) { 返回任务。运行(()=> { List res=新列表(); 字符串nextpagetoken=“”; while(nextpagetoken!=null) { var searchListRequest=_youtubeService.Search.List(“代码段”); searchListRequest.MaxResults=50; searchListRequest.ChannelId=ytChannelId; searchListRequest.PageToken=nextpagetoken; searchListRequest.Type=“视频”; //调用search.list方法检索与指定查询词匹配的结果。 var searchListResponse=searchListRequest.Execute(); //处理视频响应 res.AddRange(searchListResponse.Items); nextpagetoken=searchListResponse.nextpagetoken; } 返回res; }); }
这个方法应该会让你走上正轨

那么ClientServiceRequest.cs呢?我应该这样做吗?我已经创建了API,但在我的应用程序中,我得到了错误消息:访问未配置。您的项目未启用API(YouTube数据API)。请使用Google开发者控制台更新您的配置。[403]不需要ClientServiceRequest.cs…只需从Nuget Packet manager安装Youtube API v3即可
        public Task<List<SearchResult>> GetVideosFromChannelAsync(string ytChannelId)
    {

        return Task.Run(() =>
        {
            List<SearchResult> res = new List<SearchResult>();

            string nextpagetoken = " ";

            while (nextpagetoken != null)
            {
                var searchListRequest = _youtubeService.Search.List("snippet");
                searchListRequest.MaxResults = 50;
                searchListRequest.ChannelId = ytChannelId;
                searchListRequest.PageToken = nextpagetoken;
                searchListRequest.Type      = "video";

                // Call the search.list method to retrieve results matching the specified query term.
                var searchListResponse = searchListRequest.Execute();

                // Process  the video responses 
                res.AddRange(searchListResponse.Items);

                nextpagetoken = searchListResponse.NextPageToken;

            }

            return res;

        });
    }