Click here to Skip to main content
15,888,303 members
Please Sign up or sign in to vote.
4.80/5 (2 votes)
Hi so I've recently come around a home page that allows you to copy annotations in a youtube video on to another using some base javascript functionality how ever I'm now trying to expand upon this and create a more advance annotation creator, using C#.

The scripts can be fond on this site: http://stefansundin.com/stuff/youtube/youtube-copy-annotations.html[^] but the may part of the script I'm struggling with is:

C#
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'https://www.youtube.com/annotations_auth/update2', true);
xhr.send(data);


For now I've tried using the WebRequest and HTTPWebRequest but it keep throwing me an 403 error;

string xData = xDoc.InnerXml;
            
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(xData);

            WebRequest rq = (WebRequest)WebRequest.Create("https://www.youtube.com/annotations_auth/update2");
            rq.Method = "POST";
            
            using (Stream s = rq.GetRequestStream())
            {
                s.Write(buffer, 0, buffer.Length);
            }

            HttpWebResponse httpWebResponse = (HttpWebResponse)rq.GetResponse(); //Runtime error The remote server returned an error: (403) Forbidden. 


I've already triple - if not quadruple - checked the XML content and it should be completely identical with the one I send on the site, where the only exception may be the encoding?

So do anyone know how I can change this JavaScript into something shiny/working in C#?

Thanks a bunch in regard!
Posted

1 solution

(403) Forbidden

http://en.wikipedia.org/wiki/HTTP_403[^]

The relevant portion: A web server may return a 403 Forbidden HTTP status code in response to a request from a client for a web page or resource to indicate that the server can be reached and understood the request, but refuses to take any further action. Status code 403 responses are the result of the web server being configured to deny access, for some reason, to the requested resource by the client.

I think the javascript is using the current 'connection' and the validated page, the C# is creating a new unvalidated connection. It is to prevent malicious hacking of the web server.
 
Share this answer
 
Comments
Jackie00100 16-Jan-14 13:49pm    
I see, so there is really no way of doing the same in C#?
bowlturner 16-Jan-14 13:56pm    
While anything is possible, I think it is likely a waste of your time. You would be trying to circumvent in place security.
Jackie00100 16-Jan-14 14:59pm    
okay, I'll try an other approach, maybe use a html document and invoke the JavaScript that way, even that it sadly feels like a big hack around.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900