7 Replies - 1509 Views - Last Post: 22 September 2015 - 11:29 AM Rate Topic: -----

#1 m1m6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-July 15

Twitter does not want to log me in

Posted 21 September 2015 - 11:56 AM

Can you guys help me login into the Twitter (http://twitter.com) with a webrequest?

I have done everything I could think of,

Here is my code (I have replaced my actual username and password for Twitter with USERNAME and PASSWORD):

private void Form1_Load(object sender, EventArgs e)
    {
        HtmlNode.ElementsFlags.Remove("form");
        string url = "https://twitter.com";
        var Webget = new HtmlWeb();
        var doc = Webget.Load(url);
        CookieContainer temp = new CookieContainer();

        var value = doc.DocumentNode.SelectSingleNode("//input[@type='hidden' and @name='authenticity_token']")
             .Attributes["value"].Value;

        Console.WriteLine(value);
        HttpWebRequest req =(HttpWebRequest) WebRequest.Create("https://twitter.com/sessions");
        req.Method = "POST";
        string postData = "session%5Busername_or_email%5D=USERNAME&session%5Bpassword%5D=PASSWORD&remember_me=1&return_to_ssl=false&scribe_log=&redirect_after_login=%2F&authenticity_token=" + value.Trim();
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        req.ContentType = "application/x-www-form-urlencoded";
        req.CookieContainer = new CookieContainer();
        // Set the ContentLength property of the WebRequest.
        req.ContentLength = byteArray.Length;
         req.ContentType = "application/x-www-form-urlencoded";
    req.Referer = "https://twitter.com/sessions";
    req.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36";
    req.ContentLength = byteArray.Length;
        Stream dataStream = req.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        WebResponse response = req.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Twitter does not want to log me in

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 451
  • View blog
  • Posts: 2,188
  • Joined: 07-April 08

Re: Twitter does not want to log me in

Posted 21 September 2015 - 11:58 AM

You'll need to give us more to go on. What errors are you getting? Have you done any sort of debugging at all? What line is throwing the errors?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Twitter does not want to log me in

Posted 21 September 2015 - 12:08 PM

I'm sure their API returns a reason (a response code or error code). Check it.

This looks like a very straight-forward debugging situation.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?


WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.


FAQ (Frequently Asked Questions - Updated Sep 2015
Spoiler



Was This Post Helpful? 0
  • +
  • -

#4 m1m6   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-July 15

Re: Twitter does not want to log me in

Posted 21 September 2015 - 02:21 PM

View Postrgfirefly24, on 21 September 2015 - 11:58 AM, said:

You'll need to give us more to go on. What errors are you getting? Have you done any sort of debugging at all? What line is throwing the errors?


it's doesn't output an error it just not logged in ...
i think the problem from the cookie ?!
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Twitter does not want to log me in

Posted 21 September 2015 - 02:54 PM

Guess again.
Per the Twitter Developer API site:
https://dev.twitter....in/implementing

Quote

Your application should examine the HTTP status of the response. Any value other than 200 indicates a failure.


Details of the error codes are here:
https://dev.twitter..../response-codes
Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Twitter does not want to log me in

Posted 22 September 2015 - 06:27 AM

@tlhIn`toq, that's for Twitter's login protocol for other applications. Like you can log in to other sites with Facebook and Google? Twitter has the same thing.

The OP here isn't using their API. It looks like he's trying to simulate actually going to Twitter's site and signing in.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Twitter does not want to log me in

Posted 22 September 2015 - 11:11 AM

Well maybe that's the problem? Since he wants to Interface his Application with their Program, maybe he SHOULD be using the Application Programmer's Interface.

The fact that he's trying to simulate a user on a web page is just dirty coding and damned suspicious in my book.
Was This Post Helpful? 0
  • +
  • -

#8 ybadragon   User is offline

  • Home Owner
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,647
  • Joined: 11-May 12

Re: Twitter does not want to log me in

Posted 22 September 2015 - 11:29 AM

Not to mention it's more than likely against their TOS/TOU to automate their website via scraping and invoking methods. I had an application that scraped the MTG site (magic the gathering) and posted a question about it on here once, and Modi shut it down. At the tiime I didn't know about API's though, and apologized to Modi via an email about it. Anyway @OP, do you know what an API is, or did you know that Twitter had one?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1