Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey people so I kinda did try to ask this for some ago but never really got answered so the thing is that I'm currently trying to put the functionality from this site http://stefansundin.com/stuff/youtube/youtube-copy-annotations.html[^] into a c# app however every time I try to send the XML data to the url it returns a "403 Forbidden".

This behavior confuses me a lot since if I use the Chrome plugin called "PostMan" everything works and I get 200 OK returned EVERY time and the XML is identical also, as well the site works fine too.

I've tracked the problem to possibly have something to do with cookies not being access probably but I just can't seems to get the problem solved (as you can see from the outcommented lines)

My code is this: (If any one would like my XML data and/or full code please tell me so)

private void PostXML()
{
    byte[] bytes = Encoding.UTF8.GetBytes(richTextBox1.Text.ToString());
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.youtube.com/annotations_auth/update2/");
    //request.KeepAlive = true;
    request.Method = "POST";
    request.AllowAutoRedirect = false;
    CookieContainer cookies = new CookieContainer();
    CookieCollection cookieCollection = cookies.GetCookies(request.RequestUri);
    //cookieGroup.Add(new Cookie("VISITOR_INFO1_LIVE", "l0OlSV7lLCU", "/", ".youtube.com"));
    //cookieGroup.Add(new Cookie("YSC", "7S2fdqmzpq8", "/", ".youtube.com"));
    //cookieGroup.Add(new Cookie("PREF", "f1=50000000", "/", ".youtube.com"));
    //cookieGroup.Add(new Cookie("GEUP", "070e3c5e453bb2e5c5edb10867214dddaQMAAAA=", "/", ".youtube.com"));
    //request.CookieContainer = cookieCollection;
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(bytes, 0, bytes.Length);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //The remote server returned an error: (403) Forbidden.

    if (response.StatusCode != HttpStatusCode.OK)
    {
        string message = String.Format("POST failed. Received HTTP {0}",
        response.StatusCode);
        throw new ApplicationException(message);
    }
}

I'm getting this respond header info back when the error is thrown (However the set-cookie seems to change from time to time):

{X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
Date: Thu, 15 May 2014 13:26:37 GMT
Expires: Tue, 27 Apr 1971 19:44:06 EST
Set-Cookie: VISITOR_INFO1_LIVE=8ccNBaFb8xo; path=/; domain=.youtube.com; expires=Wed, 14-Jan-2015 01:19:37 GMT,YSC=ii4JdoC39CY; path=/; domain=.youtube.com; httponly,PREF=f1=50000000; path=/; domain=.youtube.com; expires=Wed, 14-Jan-2015 01:19:37 GMT
Server: gwiseguy/2.0
P3P: CP="This is not a P3P policy! See http://support.google.com/accounts/bin/answer.py?answer=151657&hl=da for more info."
Cache-Control: no-cache
Content-Length: 0
X-XSS-Protection: 1; mode=block
}

So I was wondering what i can do to fix this so I in my C# app can get 200 OK returned aswel, any ideas?

Thanks a lot in regard - Jackie
Posted
Updated 15-May-14 4:23am
v3
Comments
gggustafson 15-May-14 13:19pm    
Did you read http://support.google.com/accounts/bin/answer.py?answer=151657&hl=da
Jackie00100 15-May-14 16:07pm    
Yes but doesn't really seem to give a clue on what/how to fix it
gggustafson 16-May-14 0:34am    
Are you sure that you are getting this response header with the code you provided? It seems that some of your commented code appears in the header.
Jackie00100 17-May-14 22:59pm    
Yes, I read somewhere I needed to add the cookies to the request my self but it still failed
gggustafson 18-May-14 0:06am    
Use "Improve question" to modify your question so that the code reflects the response header. That way we can examine what went wrong. Eliminate all commented code (it has no bearing on your problem). Reply to this message when the code is revised.

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