HI WELCOME TO KANSIRIS

Upload a Video to YouTube From C# ASP.NET MVC

Leave a Comment

Introduction:


Hello Folks, This is the first post of my blog. Today I will Explain How to upload a video to YouTube From C# code with ASP.NET MVC. Normally we people use to upload a video for entertainment, For the blog, News, Gaming etc, But Can we upload a Video From ASP.NET MVC Web Application With C#?

Yes, There is Possibilities are there. To integrate with YouTube and working with YouTube functionalities There is YouTube API(Application Programming Interface) is there. That means Access YouTube From Program interface. Using that API we can upload video from our Web Application.

I will explain step by step. First, we will gather the required items.

Requirements :


1. Google API Key
2. Google Username(Email)
3. Google Password

Lets Go:


Step 1 : To upload video to YouTube we need to Authenticate Google account first, For that, we need above things. First, go to Google Developer Console

Step 2 : Sign in with your Google Account. Click Create Project and Enter name for your Project.



Step 3 : After creating a project it will lead you to project dashboard. In that page Click "Use Google APIs". In that API Manager, There is a list of Google APIs are there, In that Select YouTube Data API.



Step 4 : Then click Enable API. After Few Seconds you will be notified that API is enabled. Then Click "Go to Credentials". And Click Create API Key and select Browser and Create it, At finally, you will get an API key.





Step 5 : Then You need to download Google Data API and then reference to your project. Download from here Google Data APIAdd These namespaces to your controller.

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

Step 6 : Let's start the code. Create new ASP.Net MVC Project or Use the old one.
In your Controller, Copy the below method and paste it.


         [HttpPost]
        public ActionResult Create(System.Web.Mvc.FormCollection collection, YouTubeModel youtubeModel, HttpPostedFileBase file)
        {
            try
            {
                if (file != null)
                {
                    bllYoutubeUpload = new BllYoutubeUpload();
                    YouTubeRequestSettings settings;
                    YouTubeRequest request;
                    string devkey = "Your API Key Here";
                    string username = "Your GMail Here@gmail.com";
                    string password = "Your Password Here";
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
                    file.SaveAs(path);
                    string filepath = Path.GetFullPath(path);
                    settings = new YouTubeRequestSettings("API Project", devkey, username, password) { Timeout = 999999999 };
                    request = new YouTubeRequest(settings);
                    Video video = new Video();
                    video.Title = "Sample Video Title";
                    video.Description = "Sample Video Description";
                    video.Tags.Add(new MediaCategory("Comedy", YouTubeNameTable.CategorySchema));
                    video.Keywords = "Comedy";
                    video.Private = false;
                    video.MediaSource = new MediaFileSource(filepath, "video/");
                    Video createdVideo = request.Upload(video); //In that createdVideo you will get uploaded video ID.
                    return RedirectToAction("Create");
                }
            }
            catch
            {
                return View();
            }
        }

The parameter "HttpPostedFileBase file" you will get Video then Your video will be placed in a local directory and then uploaded to YouTube. After uploaded to YouTube You will get Uploaded YouTube video ID. Hope the above code is easy to understand if you can't understand or have any doubt comment in the post and I will get back to you ASAP. :)

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.