I'm trying to upload a video from an c# web application to Youtube using API v2.0.
This is the documentation i found:
YouTube API 2.0 docs
I can accomplish AuthSub for web applications and send the video metadata to youtube;
After that i can read the TOKEN and the URL needed to make the browser-based upload.
Now i've to fill this HTML form to perform the real upload.
<form action="post_url?nexturl=http://example.com" method ="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="hidden" name="token" value="token_value"/>
<input type="submit" value="go" />
</form>
I've searched the net for an example to how programatically "emulate" and send this html form from codebehind without luck.
This is my actual code only written for testing purpose:
YouTubeRequestSettings settings = new YouTubeRequestSettings("TEST_APP", "DEVELOPER_KEY", "USERNAME", "PASSWORD");
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "My Test Movie";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "cars, funny";
newVideo.Description = "My description";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag", YouTubeNameTable.DeveloperTagSchema));
FormUploadToken token_youtube = request.CreateFormUploadToken(newVideo);
string url_per_post_youtube = token_youtube.Url + "?nexturl=" + HttpContext.Current.Request.Url.AbsoluteUri + "test.aspx";
byte[] fileBytes = File.ReadAllBytes("C:\\VIDEO.MOV");
StringBuilder sb = new StringBuilder();
foreach (byte b in fileBytes)
{
sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
}
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "token=" + token_youtube.Token.ToString();
postData += ("&file=" + sb );
byte[] data = encoding.GetBytes(postData);
try
{
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(url_per_post_youtube);
myRequest.Method = "POST";
myRequest.ContentType = "multipart/form-data";
myRequest.KeepAlive = true;
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
string strResponse;
StreamReader stIn = new StreamReader(myRequest.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
}
catch (Exception ex)
{
string asd = ex.Message;
}
With this code the youtube remote server reply with:
The remote server returned an error: (400) Bad Request
A 400 response code identifies a bad request. For example, you will
receive a 400 response code if you submit a request to the wrong URL
or include an unsupported or nonexistent parameter in your request.
The API response content will explain the reason why the API returned
a 400 response code
I cannot find the way to understand what is appenin'