Search code examples
c#asp.netyoutubeyoutube-apiyoutube.net-api

400 Bad Request Error uploading videos to YouTube using C# YouTube API


I can't seem to get my ASP.net application to succesfully publish to my YouTube account but not matter what I do I get a 400 Bad Request error . I used the YouTube documentation to create my code and obtaining a dev key. Also, I am not receiving and authentication error so I believe that my parameters are correct. Despite a good deal of searching I cannot not find a definitive answer to my quests so I can only assume that I am using the API incorrectly or omitting something critical.

My using directive are as follows

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

My Code for calling the APIs is here

YouTubeRequestSettings settings = new YouTubeRequestSettings("k2ps",
  YOU_TUBE_DEVELOPER_KEY, YOU_TUBE_USER_NAME, YOU_TUBE_PASSWORD);
  YouTubeRequest request = new YouTubeRequest(settings);

  Video video = new Video();
  video.Title = title;
  video.Tags.Add(new MediaCategory(sport, YouTubeNameTable.CategorySchema));
  video.Keywords = keyword;
  video.Description = "K2ps";  //todo replace this with the preview text
  video.YouTubeEntry.Private = false;

  video.YouTubeEntry.MediaSource = new MediaFileSource(videoPath, "video/x-flv");
  Video createdVideo = request.Upload(video);

I am getting a HTTP 400 Bad request exception being thrown. I used fiddler to get the raw response which is below.

Execution of request failed: http://uploads.gdata.youtube.com/feeds/api/users/default/uploads at Google.GData.Client

   HTTP/1.1 400 Bad Request
    Server: Upload Server Built on Jun 6 2011 12:48:11 (1307389691)
    X-GData-User-Country: US
    Content-Type: text/xml; charset=UTF-8
    Date: Thu, 09 Jun 2011 02:07:30 GMT
    Pragma: no-cache
    Expires: Fri, 01 Jan 1990 00:00:00 GMT
    Cache-Control: no-cache, no-store, must-revalidate
    Content-Length: 257



        <?xml version='1.0' encoding='UTF-8'?>
           <errors>
              <error>
                <domain>yt:validation</domain>
                <code>invalid_value</code>
         <location type='xpath'>
 media:group/media:category[@scheme='http://gdata.youtube.com/schemas/2007/categories.cat']/text()</location>
              </error>
          </errors>

Solution

  • According to the response, your media:category might be incorrect. Try:

    video.Tags.Add(new MediaCategory("Sports", YouTubeNameTable.CategorySchema));