Is it possible to get all videos from a channel (not mine)? If it is possible, can I use a simple API key or should I use OAuth 2.0 credentials?
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.
Follow this link https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads
You can use your API key and just query all channels video ( even if not yours :) )
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;
});
}
This method should get you on track
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With