从频道获取所有视频-Youtube API v3 c# [英] Get all videos from channel - Youtube API v3 c#

查看:74
本文介绍了从频道获取所有视频-Youtube API v3 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to get all videos from some channel (not mine)? If it is possible, can I use a simple api key or should I use OAuth 2.0 credentials?

推荐答案

我以这种方式完成了工作,并且对我有用 我使用了Nuget Packet Manager中的Youtube API v3

I have done in this way and it worked for me I have used Youtube API v3 from Nuget Packet manager

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");
}

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

Provide your channel name in this line

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

关注此链接 https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads

这篇关于从频道获取所有视频-Youtube API v3 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆