There is a task to link cUrl and C # and make authorization for instagram. I create dynamically * .bat files and run them in the console, but I cannot login to the site. Tell me what could be the problem?

string batnik = "curl --cookie-jar cookies.txt"; batnik += " \"https://www.instagram.com/\""; batnik += " -D header.txt"; batnik += " -H \"accept-encoding: gzip, deflate, sdch\" "; batnik += " -H \"accept-language: uk,ru;q=0.8,en-US;q=0.6,en;q=0.4\""; batnik += " -H \"Cookie: csrftoken = " + Token + "; s_network=; ig_pr=1; ig_vw=1280; mid = " + this.mid + "\""; batnik += " -H \"upgrade-insecure-requests: 1\""; batnik += " -H \"User - Agent: " + UserAgent + "\""; batnik += " -H \"accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*; q = 0.8\""; batnik += " -H \"Referer: https://www.instagram.com/\""; batnik += " -H \"cache-control: max-age=0 \""; batnik += " -H \"authority: www.instagram.com\""; batnik += " --compressed >> " + user + "-first.txt"; File.WriteAllText(user + "-first.bat", batnik); Process.Start("cmd", " /C " + user + "-first.bat"); 

After that I create and run the following * .bat file.

 string batnik = "curl --cookie-jar cookies.txt"; batnik += " \"https://www.instagram.com/accounts/login/ajax/\""; batnik += " -D header.txt"; batnik += " -H \"Cookie: csrftoken = " + Token + "; mid = " + this.mid + ";\""; batnik += " -H \"Origin: https://www.instagram.com\""; batnik += " -H \"Accept - Encoding: gzip, deflate\""; batnik += " -H \"accept-language: uk,ru;q=0.8,en-US;q=0.6,en;q=0.4\""; batnik += " -H \" user-agent: " + UserAgent + "\""; batnik += " -H \"x - requested - with: XMLHttpRequest\" "; batnik += " -H \"X - CSRFToken: " + Token + "\""; batnik += " -H \"X - Instagram - AJAX: 1\""; batnik += " -H \"Content - Type: application / x - www - form - urlencoded; charset = UTF - 8\""; batnik += " -H \"Accept: */*\" -H \"Referer: https://www.instagram.com/\""; batnik += " -H \"authority: www.instagram.com\" "; batnik += " --data \"username = " + user + " & password = " + pass + "\" --compressed >> " + user + "-login.txt"; File.WriteAllText(user + "-login.bat", batnik); Process.Start("cmd", " /C " + user + "-login.bat"); 

So far, so, but I still can't login. Help who than can. PS PHP does not offer, already have a working version, you need exactly C # + cUrl.

    1 answer 1

    Can it be easier without CURL? :)

    Almost the same as yours, only using RestSharp :

    1. Create a client instance, add a container for cookies, assign a favorite User Agent from a constant:

       var client = new RestClient("https://www.instagram.com/"); client.CookieContainer = new CookieContainer(); client.UserAgent = USER_AGENT; 
    2. We make the first request, the purpose of which is to get a csrf token from cookies:

       var firstRequest = new RestRequest("/", Method.GET); var firstResponse = client.Execute(firstRequest); var csrftoken = firstResponse.Cookies.First(x => x.Name == "csrftoken").Value; 
    3. We send the login / password, and in response we get JSON with the status:

       var loginRequest = new RestRequest("/accounts/login/ajax/", Method.POST); loginRequest.AddHeader("X-CSRFToken", csrftoken); loginRequest.AddHeader("X-Requested-With", "XMLHttpRequest"); loginRequest.AddHeader("X-Instagram-AJAX", "1"); loginRequest.AddHeader("Referer", client.BaseUrl.ToString()); loginRequest.AddParameter("username", username); loginRequest.AddParameter("password", password); var loginResponse = client.Execute<LoginResponse>(loginRequest).Data; 

      The LoginResponse class used to deserialize and store the server response:

       public class LoginResponse { public string status { get; set; } public bool authenticated { get; set; } } 
    4. Hooray! loginResponse.authenticated is True - User is authenticated! We can continue to make some requests from under this user to instagram, using our client .
    • Good day! And do not give, please, an example, how now to make a request from under this user? Here is the code: var selfRequest = new RestRequest ("/ users / self", Method.GET); var selfResponse = client.Execute (selfRequest); does not work, returns the status "Not found". - Vaskrol