Youtube Api v3 .Net Tutorial with code Examples

by Azeem Tariq

Posted 2 years ago

Last Updated 1 year ago


Youtube Api v3 .Net Tutorial with code Examples

In this article we are going to see show to use YouTube API v3 .Net . This article is not going to cover all aspects of YouTube API v3 but it will helps you to get started with YouTube API. What is API ? well , API stands for Application Programmer interface.If you want to communicate with YouTube servers then you have to use API that YouTube provide in order to get the job done.

Pre Requirements for working with YouTube API V3 .Net are:

  • Visual Studio 2010 or higher.
  • json Framework for .net.
  • Basic Programming Knowledge of C# Language.

ok lets get start Open Visual Studio and create new project.  

create new project

Create new project in Visual Studio i am going to select console application for this article so that we can focus only on YouTube API .I recommend you to do the same because you are learning API.  

Select C# Language and Project Type

Select C# Language and Project Type Now we need to download json framework. In Tools Menu Item hover your cursor on NuGet Package Manager and click on Manage NuGet Packages for Solution. [caption id="attachment_682" align="aligncenter" width="391"]

Manage Nuget Packages for solution

Manage Nuget Packages for solution[/caption] if you do not get NuGet Package Manager item in Tools Menu Item , it means NuGet Package Manager is not installed on your system. To install NuGet Package Manager Again go to Tools menu and click on Extensions Manager.  

Extention Managers

Search for NuGet Package Manager , click on download.  

NuGet Package Manager install

it will download and install NuGet Package Manager, after installing Restart Visual Studio. Now you should get NuGet Package Manager item in Tools Menu item. Type json in Search Field. Select and click on install.  

install json in Youtube Api v3 .Net project

Select your project in which you want to install json framework  

json install on Youtube Api v3 .Net project

Add these two lines at top of your code

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Youtube api tutorial v3

Now you are ready to communicate with YouTube servers using YouTube API v3. Check out how to get data from json object. YouTube Official Documentation Here is complete example code

using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Data; using System.Linq; using System.Net; /* Author : Azeem Tariq website : Email: [email protected] Do , what ever you want to do with this code :) */ namespace youtubeapi { class Program { static void Main(string[] args) { Console.WriteLine("Enter Channel UserName To Search Videos"); string username = Console.ReadLine(); displayChannel(getChannel(username)); displayVideos(getVideos(username)); Console.ReadKey(); } private static void displayVideos(List videos) { foreach(var video in videos) { Console.WriteLine("=================================================n"); Console.WriteLine("Published Date : " + video.publishedDate + "n"); Console.WriteLine("Link : " + + "n"); Console.WriteLine("Title : "+video.title+"n"); Console.WriteLine("Views : " + video.views + "n"); Console.WriteLine("Comments : " + video.comments + "n"); } } private static void displayChannel(Channel aChannel) { Console.WriteLine("============== Channel Information ===============n"); Console.WriteLine("Total Videos :"+aChannel.videosCount+"n"); Console.WriteLine("Total Subscribers :" + aChannel.subsribers + "n"); Console.WriteLine("Life Time Views :" + aChannel.viewsCount + "n"); } private static List getVideos(string userName) { List newVideolist = new List

(); // video list to hold videos WebClient wC = new WebClient { Encoding = Encoding.UTF8 }; // creating WebClient object to download json string // downloading json string string jsonString = wC.DownloadString("" + userName + "/uploads/?v=2&alt=json&max-results=10"); // max result are defiend in this requested string JObject jObj = (JObject)JsonConvert.DeserializeObject(jsonString); // converting json string into json object foreach (var entry in jObj["feed"]["entry"]) // getting data from json object { string title = entry["title"]["$t"].ToString(); string comments = entry["gd$comments"]["gd$feedLink"]["countHint"].ToString(); string count = entry["yt$statistics"]["viewCount"].ToString(); string publishedDate = entry["published"]["$t"].ToString(); // how to get data from json object ? check this link for more details newVideolist.Add(new Video(title, comments, count, publishedDate )); // creating video object and adding it into our video list } return newVideolist; } private static Channel getChannel(string userName) { Channel mychannel = null; JObject jvideo = null; JObject jCh = null; WebClient wc = new WebClient { Encoding = Encoding.UTF8 }; string chInfo= wc.DownloadString("" + userName + "?alt=json"); string videoInfo = wc.DownloadString("" + userName + "/uploads?alt=json"); jvideo = (JObject)JsonConvert.DeserializeObject(videoInfo); jCh = (JObject)JsonConvert.DeserializeObject(chInfo); string videosCount = jvideo["feed"]["openSearch$totalResults"]["$t"].ToString(); string subsribers = jCh["entry"]["yt$statistics"]["subscriberCount"].ToString(); string viewsCount = jCh["entry"]["yt$statistics"]["totalUploadViews"].ToString(); mychannel = new Channel(videosCount, subsribers, viewsCount); return mychannel; } } public class Video { public string title { get; set; } public string comments { get; set; } public string views { get; set; } public string publishedDate { get; set; } public string link { get; set; } public Video(string title ,string comments , string views , string publishedDate ) { this.title = title; this.comments = comments; this.views = views; this.publishedDate = publishedDate; } } public class Channel { public string videosCount { get; set; } public string subsribers { get; set; } public string viewsCount { get; set; } public Channel(string videosCount, string subsribers, string viewsCount) { this.videosCount = videosCount; this.viewsCount = viewsCount; this.subsribers = subsribers; } } }

comment below if you have any problem i will try to solve your issue. and do not forget follow us :)