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

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. :)

Upload a Video to YouTube From C# ASP.NET MVC Upload a Video to YouTube From C# ASP.NET MVC Reviewed by Dot Net Geek on 11:30 PM Rating: 5

26 comments:

  1. hi,very nice article.
    after uses i am getting error: Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads

    ReplyDelete
    Replies
    1. Make sure you have configured correct API Key from Google Console.

      Refer : http://stackoverflow.com/questions/15056612/youtube-api-upload-is-not-working

      Delete
  2. Replies
    1. Once this line executed. You will get Video ID in "createdVideo" variable.

      "Video createdVideo = request.Upload(video);"

      Delete
  3. i'm getting error for YouTubeModel youtubeModel..it shows the type or name space you r missing.. so which dll i have to import to avoid this error

    ReplyDelete
    Replies
    1. It's my MVC Model class name. Remove that(YouTubeModel youtubeModel) from your code. Its not mandatory to have Model in parameter. Let me know if it solves your error or Any other problem uploading Youtube video?

      Thanks.

      Delete
  4. plz provide a total code as a zip file..

    ReplyDelete
    Replies
    1. Soon I will create a console application for this and let you know. Thanks.

      Delete
  5. I'm getting error at Video createdVideo = request.Upload(video);its showing like dis...
    (An exception of type 'Google.GData.Client.GDataRequestException' occurred in Google.GData.Client.dll but was not handled in user code)..so what can i do to remove this error..

    ReplyDelete
    Replies
    1. Hi Keerthana,

      The error you are getting is not meaningful. Please use try catch block and debug the code. And tell me what error are you getting at that line?

      Delete
  6. Please send reply as soon as possible...

    ReplyDelete
  7. I did as u tell..using try catch block..In debugging mode debugger stops at this line Video createdVideo = request.Upload(video); and it is showing like that...

    ReplyDelete
    Replies
    1. Hi Keerthana,

      Seems they depreciated Google GData and moved to OAuth environment. So I have to update this post or create new post for uploading Youtube video with OAuth 2.0. Since I'm not sure when will I post about this.

      Refer : http://stackoverflow.com/a/30497154

      Seems there is workaround for GData try this:

      http://stackoverflow.com/questions/13664731/accessing-older-gdata-apis-spreadsheet-api-using-oauth-2-and-a-service-account

      Thanks.

      Delete
    2. Near to Video createdVideo = request.Upload(video) i'm getting an exeption like this ..Additional information: Execution of authentication request returned unexpected result: 404 how can i rectify this...

      Delete
    3. Hello,

      Sorry about that. Refer my above comment : https://codewithgeek.blogspot.in/2015/12/upload-video-to-youtube-from-c-aspnet.html?showComment=1481610731146#c7053966665831331255

      Seems this is not working now and I will write new blog post about upload video via OAuth V2 using Access token. Stay tuned!

      Thanks :)

      Delete
  8. Getting Exception as invalid credentials at Video createdVideo = request.Upload(video);

    ReplyDelete
  9. i am getting error here setiings and request is not taking

    ReplyDelete
  10. it is showing
    "name settings does not exist in the current context"
    "name request does not exist in the current context"

    ReplyDelete
  11. i am getting a error at last line
    Execution of authentication request returned unexpected result: 404

    ReplyDelete
  12. It is very good blog and useful for students and developer , Thanks for sharing



    .Net Online Training

    ReplyDelete
  13. I've created a Video Tutorial on how to upload video on YouTube with the usage of C# (.NET Core).
    Watching this video will help you to solve your issue!
    All documentation as well as source code is here!

    ReplyDelete

Powered by Blogger.