Category : C# | Author : Chtiwi Malek | First posted : 10/23/2012 | Updated : 12/13/2012
Tags : youtube, .net, c#, upload, video, videos, api, google, delete, guide, how to
Upload a Video to YouTube with C# .Net

Upload a Video to YouTube with C# .Net

My last client project was an audio/video c# application where users upload their educational videos, audios, notes…  The website basically stores the uploaded videos on Youtube and the users watch the embedded video via an enhanced custom player. That’s cool, no need for video storage servers and huge bandwidth.

In this entry I’m sharing the process and functions I wrote to get a video file uploaded to (or Deleted from) Youtube.

First, to interact with Youtube we will use the Api provided by google for the .Net environment.

If you haven’t already downloaded the “Google_Data_Api” here’s the Link :
http://code.google.com/p/google-gdata/downloads/list

Three Dlls need to be referenced in our project (Google.GData.Client.dll, Google.GData.Extensions.dll, Google.GData.YouTube.dll). Click on add references and search for them, or just copy the files to the Bin directory.

The next step is Getting a Youtube Api Developper Key, to obtain a developer key visit http://code.google.com/apis/youtube/dashboard/

Now we can start coding: add these classes declaration:
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
using Google.GData.Client;
and here’s the function you need to upload a video to Youtube:
public static string UploadVideo(string FilePath,string Title, string Description)
{
   YouTubeRequestSettings settings;
   YouTubeRequest request;
   string devkey = "YOUR DEVELOPPER KEY HERE";
   string username = "Your Youtube Username";
   string password = "Your Youtube Password";
   settings = new YouTubeRequestSettings("Your Application Name", devkey, username, password) {Timeout = -1};
   request = new YouTubeRequest(settings);
	        
   Video newVideo = new Video();
	
   newVideo.Title = Title;
   newVideo.Description = Description;
   newVideo.Private = true;
   newVideo.YouTubeEntry.Private = false;
	
   //newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
	       
   //newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));
	
   //newVideo.YouTubeEntry.setYouTubeExtension("location", "Paris, FR");
   // You can also specify just a descriptive string ==>
   // newVideo.YouTubeEntry.Location = new GeoRssWhere(71, -111);
   // newVideo.YouTubeEntry.setYouTubeExtension("location", "Paris, France.");
	
   newVideo.YouTubeEntry.MediaSource = new MediaFileSource(FilePath, "video/mp4");
   Video createdVideo = request.Upload(newVideo);
	
   return createdVideo.VideoId;
}
The previous function will return the Video ID, you should store that string because you’ll need it to access, play, delete... the video later.

The next function will delete a video, you’ll need to provide the Video ID :
public static bool DeleteVideo(string VideoId)
{
   try
   {
      YouTubeRequestSettings settings;
      YouTubeRequest request;
      string devkey = "YOUR DEVELOPPER KEY HERE";
      string username = "Your Youtube Username";
      string password = "Your Youtube Password";
      settings = new YouTubeRequestSettings("Your Application Name", devkey, username, password) { Timeout = -1 };
      request = new YouTubeRequest(settings);
	
      Uri videoEntryUrl = new Uri(String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads/{1}", username, VideoId));
      Video video = request.Retrieve<Video>(videoEntryUrl);
      request.Delete(video);
	
      return true;
   }
   catch (Exception ex)
   {
      return false;
   }
}
You can extend this code to include other actions (Updating a video, retrieving standard video feeds, video comments, responses... ), for more check https://developers.google.com/youtube/2.0/developers_guide_protocol

if you like this tutorial, please Share it.
Have fun and don't hesitate to comment or ask questions.
About the author :
Malek Chtiwi is the man behind Codicode.com
34 years old full stack developer.
Loves technology; but also likes design, photography and composing music.
Comments & Opinions :
Fantastic
Fantastic
- by Wdwsd on 2/15/2013
send me the sample code
plz send me the sample code
- by govinda on 8/1/2013
Uploading Video to YouTube Using API(V3)
When I go and click on the developer key link, all it does is ask me to sign in, and then goes to a 404 Not Found? Got an updated link?
- by Micah on 11/19/2014
Not Work
throw error: Execution of authentication request returned unexpected result: 404
- by kendy on 2/24/2017
Leave a Comment:
Name :
Email : * will not be shown
Title :
Comment :