Upload a Video to YouTube From C# ASP.NET MVC
Introduction:
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 API. Add 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
Reviewed by Dot Net Geek
on
11:30 PM
Rating:
hi,very nice article.
ReplyDeleteafter uses i am getting error: Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads
Make sure you have configured correct API Key from Google Console.
DeleteRefer : http://stackoverflow.com/questions/15056612/youtube-api-upload-is-not-working
How can i get my video Id?
ReplyDeleteOnce this line executed. You will get Video ID in "createdVideo" variable.
Delete"Video createdVideo = request.Upload(video);"
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
ReplyDeleteIt'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?
DeleteThanks.
plz provide a total code as a zip file..
ReplyDeleteSoon I will create a console application for this and let you know. Thanks.
DeleteI'm getting error at Video createdVideo = request.Upload(video);its showing like dis...
ReplyDelete(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..
Hi Keerthana,
DeleteThe 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?
Please send reply as soon as possible...
ReplyDeleteI 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...
ReplyDeleteHi Keerthana,
DeleteSeems 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.
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...
DeleteHello,
DeleteSorry 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 :)
Thank you naveen
ReplyDeleteHi Keerthana,
DeleteWelcome, Keep reading :)
Getting Exception as invalid credentials at Video createdVideo = request.Upload(video);
ReplyDeletei am getting error here setiings and request is not taking
ReplyDeleteplease reply
ReplyDeleteit is showing
ReplyDelete"name settings does not exist in the current context"
"name request does not exist in the current context"
i am getting a error at last line
ReplyDeleteExecution of authentication request returned unexpected result: 404
It is very good blog and useful for students and developer , Thanks for sharing
ReplyDelete.Net Online Training
I've created a Video Tutorial on how to upload video on YouTube with the usage of C# (.NET Core).
ReplyDeleteWatching this video will help you to solve your issue!
All documentation as well as source code is here!
MMORPG
ReplyDeleteÄ°NSTAGRAM TAKÄ°PCÄ° SATIN AL
tiktok jeton hilesi
tiktok jeton hilesi
antalya saç ekimi
referans kimliÄŸi nedir
instagram takipçi satın al
İnstagram takipçi
Mt2 Pvp Serverlar
özel ambulans
ReplyDeleteyurtdışı kargo
nft nasıl alınır
lisans satın al
en son çıkan perde modelleri
uc satın al
minecraft premium
en son çıkan perde modelleri