Search code examples
.netc#-4.0google-apiyoutubeyoutube-api

Uploading large video file(mp4, 130 megabyte) to YouTube with ResumableUploader


I have following code:

var newVideo = new Video
{
    Title = Path.GetFileNameWithoutExtension(file),
    Keywords = keywordstring,
    Description = "Some Description"
};

newVideo.Tags.Add(new MediaCategory(projectSettings.Category.ToString(), YouTubeNameTable.CategorySchema));
newVideo.YouTubeEntry.Private = false;
newVideo.YouTubeEntry.MediaSource = new MediaFileSource(file, "video/quicktime");


        AtomLink link = new AtomLink("http://uploads.gdata.youtube.com/resumable/feeds/api/users/" + Credentials.UserName.Split('@')[0] + "/uploads");
        link.Rel = ResumableUploader.CreateMediaRelation;
        newVideo.YouTubeEntry.Links.Add(link);

        ResumableUploader ru = new ResumableUploader(10);
        GDataCredentials credentials = new GDataCredentials(Credentials.UserName, Credentials.Pass);


        Authenticator youTubeAuthenticator = new ClientLoginAuthenticator("OnlineVideoDrive",ServiceNames.YouTube, credentials);
        youTubeAuthenticator.DeveloperKey = "AIzaSyCdvck_1yrovHGusir2Ddw8KlYvl7rj2LQ";

        
        ru.Insert(youTubeAuthenticator, newVideo.YouTubeEntry);

Evertything seems to be working fine until the last Line

ru.Insert(youTubeAuthenticator, newVideo.YouTubeEntry);

The line throws a WebException:

System.Net.WebException ist aufgetreten.
  HResult=-2146233079
  Message=Der Remoteserver hat einen Fehler zurückgegeben: (400) Ungültige Anforderung.
  Source=System
  StackTrace:
       bei System.Net.HttpWebRequest.GetResponse()
       bei Google.GData.Client.ResumableUpload.ResumableUploader.InitiateUpload(Uri resumableUploadUri, Authenticator authentication, AbstractEntry entry, String httpMethod)
       bei Google.GData.Client.ResumableUpload.ResumableUploader.InitiateUpload(Uri resumableUploadUri, Authenticator authentication, AbstractEntry entry)
       bei Google.GData.Client.ResumableUpload.ResumableUploader.Insert(Authenticator authentication, AbstractEntry payload, AsyncData data)
       bei Google.GData.Client.ResumableUpload.ResumableUploader.Insert(Authenticator authentication, AbstractEntry payload)
       bei LetsPlayHelper.Form1.youtubeBackgroundWorker_DoWork(Object sender, DoWorkEventArgs e) in f:\Project\Form1.cs:Zeile 348.
  InnerException: 

I already tried a direct upload with the YouTubeRequest class. Well small Videos are not a problem but larger ones are causing the Request to abort.

Edit: ex.Response.StatusCode and StatusDescription are "BadRequest"

Edit 2 : The whole upload code is running in a BackGroundWorker

Edit 3 : I also tried the YouTubeUploader example with the same error(https://code.google.com/p/google-gdata/wiki/YouTubeUploader)

I grabbed the response with Fiddler

Discovered:

<errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>InvalidRequestUriException</code><internalReason>Exception message unavailable</internalReason></error></errors>

Solution

  • I have a suggestion for you which will help you solve this problem and also future problems with Youtube API.

    1. Download Fiddler.
    2. Monitor the web request that is being done to Youtube.
    3. Youtube will return a descriptive response along with the HTTP 400 status code.
    4. Descriptive response will give you the answer. Voilà!

    It would be much easier for people to help you if you follow the steps above and put the Youtube response in your question.