YouTube API with Asp.net C#

aspnet youtubeYouTube is a major video hosting website. IF you want to have videos on your website and you don’t want to host them, you can always use YouTube as a CDN. 1st method is to upload the video on YouTube and embed the code on your website, 2nd method is to make your website directly upload the videos to YouTube and embed them. YouTube provides a .Net Api to do this from your website directly or from your application.

Here are the major functions needed to manipulate your YouTube account using ASP.net C#. To do so you must first have a YouTube account and download the YouTube Asp.net API

Add Reference to the following DLLS

  • Google.GData.Client.dll
  • Google.GData.Extensions.dll
  • Google.GData.YouTube.dll

Remember to include the following classes 

using Google.GData.Client;
using Google.GData.Extensions.Location;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.YouTube;

First start by setting some configuration.

<add key=”YouTubeDeveloperKey” value=”get that from youtube”/>
<add key =”YoutubeUserName” value=”youtube username”/>
<add key =”YoutubePassword” value=”youtube password”/>
<add key =”YouTubeCompany” value=”youtube user not the email EX: tannouswissam”/>

Now call those configuration from the code behind

private static readonly string YouTubeDeveloperKey = ConfigurationManager.AppSettings[“YouTubeDeveloperKey”];

private static readonly string YouTubeCompany = ConfigurationManager.AppSettings[“YouTubeCompany”];

private static readonly string YoutubeUserName = ConfigurationManager.AppSettings[“YoutubeUserName”];

private static readonly string YoutubePassword = ConfigurationManager.AppSettings[“YoutubePassword”];

Functions to manipulate your YouTube account using ASP.net C#

Get YouTube Videos of a user using Asp.net C#

public static Feed<Video> GetVideoByUser()
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(“Logicum”, YouTubeDeveloperKey);
YouTubeRequest request = new YouTubeRequest(settings);

// getting the videos
string videoFeedString = “http://gdata.youtube.com/feeds/api/users/” + YouTubeCompany + “/uploads”;
Uri videoFeedURI = new Uri(videoFeedString);
Feed<Video> videoFeed = request.Get<Video>(videoFeedURI);

return videoFeed;
}

Get All YouTube Playlists for a user using Asp.net C#

public static Feed<Playlist> GetAllPlayLists()
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(“Logicum”, YouTubeDeveloperKey);
YouTubeRequest request = new YouTubeRequest(settings);

Feed<Playlist> userPlaylists = request.GetPlaylistsFeed(YouTubeCompany);
return userPlaylists;
}

Creating a YouTube Playlist using ASP.net C#

public static Playlist CreatePlaylist(string title, string summary)
{
if (string.IsNullOrEmpty(title) && string.IsNullOrEmpty(summary))
{
throw new ArgumentNullException(“title and/or summary is null or empty”);
}

YouTubeRequestSettings settings = new YouTubeRequestSettings(“Logicum”, YouTubeDeveloperKey, YoutubeUserName, YoutubePassword);
YouTubeRequest request = new YouTubeRequest(settings);

Playlist pl = new Playlist();
pl.Title = title;
pl.Summary = summary;
try
{
Playlist createdPlaylist = request.Insert(new Uri(“http://gdata.youtube.com/feeds/api/users/default/playlists”), pl);

return createdPlaylist;
}
catch (Exception ex)
{
throw new Exception(“Exception in function CreatePlaylist:” + ex.Message);
}
}

Create YouTube Video using ASP.net C#

public static Video CreateYoutubeVideo(string title, string keywords, string description, bool isPrivate, byte[] content, string fileName, string contentType)
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(“Logicum”, YouTubeDeveloperKey, YoutubeUserName, YoutubePassword);
YouTubeRequest request = new YouTubeRequest(settings);

Video newVideo = new Video();

newVideo.Title = title;
newVideo.Tags.Add(new MediaCategory(“Autos”, YouTubeNameTable.CategorySchema));
newVideo.Keywords = keywords;
newVideo.Description = description;
newVideo.YouTubeEntry.Private = isPrivate;
newVideo.Tags.Add(new MediaCategory(“mydevtag, anotherdevtag”, YouTubeNameTable.DeveloperTagSchema));

// alternatively, you could just specify a descriptive string newVideo.YouTubeEntry.setYouTubeExtension(“location”, “Mountain View, CA”);
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);

Stream stream = new MemoryStream(content);
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(stream, fileName, contentType);
Video createdVideo = request.Upload(newVideo);

return createdVideo;
}

Adding YouTube Video to PlayList using ASP.net C#

public static void AddVideoToPlayList(Video video, Playlist playlist)
{
YouTubeRequestSettings settings = new YouTubeRequestSettings(“Logicum”, YouTubeDeveloperKey, YoutubeUserName, YoutubePassword);
YouTubeRequest request = new YouTubeRequest(settings);

PlayListMember pm = new PlayListMember();
pm.VideoId = video.VideoId;

try
{
request.AddToPlaylist(playlist, pm);
}
catch (Exception ex)
{
throw new Exception(“Exception in function AddVideoToPlayList:” + ex.Message);
}
}

Get YouTube Embed as a control in ASP.net C#

public static Control GetYouTubeVideoPlayer(string videoID, string width, string height)
{
if (string.IsNullOrEmpty(videoID))
{
throw new ArgumentNullException(videoID);
}

if (string.IsNullOrEmpty(width))
{
throw new ArgumentNullException(width);
}

if (string.IsNullOrEmpty(height))
{
throw new ArgumentNullException(height);
}

HtmlGenericControl playerHtml = new HtmlGenericControl(“iframe”);
playerHtml.Attributes.Add(“width”, width);
playerHtml.Attributes.Add(“height”, height);
playerHtml.Attributes.Add(“src”, “//www.youtube.com/embed/” + videoID);
playerHtml.Attributes.Add(“frameborder”, “0”);
playerHtml.Attributes.Add(“allowfullscreen”, “true”);
return playerHtml;
}

Hope this article was useful to you, Happy programming, 🙂

3 Replies to “YouTube API with Asp.net C#”

Leave a Reply