jack jor

jack jor

  • NA
  • 3
  • 3.5k

This request requires matching csrf cookie and header error

May 6 2017 10:56 AM
 I can't follow on twitter with my bot anymore!
 
Twitter changed it's algorithm and now it just uses APIs...
So my new code is like this, and it gives this error:
"This request requires a matching csrf cookie and header. 353 code error on twitter"

Here's the code:
  1. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/1.1/friendships/create.json");  
  2. request.Accept = "application/json, text/javascript, */*; q=0.01";  
  3.                             request.Headers.Add("x-twitter-auth-type""OAuth2Session");  
  4.                             request.Headers.Add("x-csrf-token", token);  
  5.                             request.Headers.Add("X-Twitter-Active-User""yes");  
  6.                             request.Headers.Add("Origin""https://twitter.com");  
  7.                             request.Headers.Add("Authorization""Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw");  
  8.                             ce.Add(new Cookie("ct0", token) { Domain = "api.twitter.com" });                          
  9.   
  10.                             request.Host = "api.twitter.com";  
  11.                             string postdata = "challenges_passed=false&handles_challenges=1&include_blocked_by=true&include_blocking=true&include_can_dm=true&include_followed_by=true&include_mute_edge=true&skip_status=true&user_id=" + datauserid;  
  12.                              
  13.                             request.Method = "POST";  
  14.                             request.Timeout = 10000;  
  15.                             request.UserAgent = useragentMozilla;  
  16.                             request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";  
  17.                             request.CookieContainer = ce;  
  18.                             request.Referer = "https://twitter.com/";  
  19.                             byte[] bytes = Encoding.UTF8.GetBytes(postdata);  
  20.                             request.ContentLength = bytes.Length;  
  21.                             Stream stream = request.GetRequestStream();  
  22.                             stream.Write(bytes, 0, bytes.Length);  
  23.                             res = (HttpWebResponse)request.GetResponse();  
  24.   
  25.                             stream.Close();  
  26.                             res.Close(); 
 
I know I'm doing something wrong in this code. I think that Bearer string never changes on twitter, it's always the same. But what is that crsf cookie and header issue. Please help me ASAP.

Maybe if I get the right token from the cookies which is "ct0" in the cookies, and add it to the header as csrf token, it can successfully follow then. But I really don't know how to get a specific cookie with C# httpwebrequest.