How to Create a Program to Login to Twitter in Visual Basic

Language
Introduction: Welcome to a tutorial on how to create a simple program to login to Twitter. This bot will not do anything other than login so you must know bot basics to carry on for performing actions. Steps of Creation: Step 1: I have created a simple form with just a button1 which is used for starting the process; Textbox1 to contain the username/email and Textbox2 for the password. You will also want to import the following...
  1. Imports System.IO
  2. Imports System.Text
  3. Imports System.Net
  4. Imports System.Text.RegularExpressions
Step 2: So... first we want to set a function called getBetween to produce a String between two points within another string. We also want to make a function to get a token from Twitter's login page...
  1. Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
  2.     Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
  3. End Function
  4. Private Function getToken()
  5.     Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.twitter.co.uk")
  6.     Dim re As HttpWebResponse = r.GetResponse()
  7.     Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
  8.     Return GetBetween(src, "<input type=""hidden"" name=""authenticity_token"" value=""", """")
  9. End Function
Step 3: Next, we want to set a lot of information to do with Twitter such as the login page, the post data etc. This has to be the same so I won't bother explaining every part of it.
  1. Dim loginCookies As CookieContainer
  2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3.     Dim token As String = getToken()
  4.     Dim postData As String = "session%5Busername_or_email%5D=" & TextBox1.Text & "&session%5Bpassword%5D=" & TextBox2.Text & "&remember_me=1&return_to_ssl=false&scribe_log=&redirect_after_login=%2F&authenticity_token=" & token
  5.     Dim tempCookies As New CookieContainer
  6.     Dim encoding As New UTF8Encoding
  7.     Dim byteData As Byte() = encoding.GetBytes(postData)
  8.      Dim r As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://twitter.com/sessions"), HttpWebRequest)
  9.     r.KeepAlive = True
  10.     r.Method = "POST"
  11.     r.CookieContainer = tempCookies
  12.     r.ContentType = "application/x-www-form-urlencoded"
  13.     r.Referer = "https://twitter.com/sessions"
  14.     r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  15.     r.ContentLength = byteData.Length
  16.     Dim re As Stream = r.GetRequestStream()
  17.     re.Write(byteData, 0, byteData.Length)
  18.     re.Close()
  19.     Dim res As HttpWebResponse = DirectCast(r.GetResponse(), HttpWebResponse)
  20.     tempCookies.Add(res.Cookies)
  21.     loginCookies = tempCookies
  22.     Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
  23. End Sub
As you can clearly see, the data being sent is setting the textbox1.text to the username/email and textbox2.text to the password. Step 4 Finally we want to check if the returned source code contains "view my profile page", if it does, we're logged in!
  1. If (src.Contains("View my profile page")) Then
  2.         loginCookies = tempCookies
  3.     MsgBox("Success!")
  4. Else
  5.     tempCookies = Nothing
  6.     MsgBox("Failed.")
  7. End If
Project Complete! Below is the full source code and download to the project files.
  1. Imports System.IO
  2. Imports System.Text
  3. Imports System.Net
  4. Imports System.Text.RegularExpressions
  5. Public Class Form1
  6.     Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
  7.         Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
  8.     End Function
  9.     Dim loginCookies As CookieContainer
  10.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  11.         Dim token As String = getToken()
  12.         Dim postData As String = "session%5Busername_or_email%5D=" & TextBox1.Text & "&session%5Bpassword%5D=" & TextBox2.Text & "&remember_me=1&return_to_ssl=false&scribe_log=&redirect_after_login=%2F&authenticity_token=" & token
  13.         Dim tempCookies As New CookieContainer
  14.         Dim encoding As New UTF8Encoding
  15.         Dim byteData As Byte() = encoding.GetBytes(postData)
  16.  
  17.         Dim r As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://twitter.com/sessions"), HttpWebRequest)
  18.         r.KeepAlive = True
  19.         r.Method = "POST"
  20.         r.CookieContainer = tempCookies
  21.         r.ContentType = "application/x-www-form-urlencoded"
  22.         r.Referer = "https://twitter.com/sessions"
  23.         r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  24.         r.ContentLength = byteData.Length
  25.         Dim re As Stream = r.GetRequestStream()
  26.         re.Write(byteData, 0, byteData.Length)
  27.         re.Close()
  28.         Dim res As HttpWebResponse = DirectCast(r.GetResponse(), HttpWebResponse)
  29.         tempCookies.Add(res.Cookies)
  30.         loginCookies = tempCookies
  31.         Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
  32.         If (src.Contains("View my profile page")) Then
  33.             loginCookies = tempCookies
  34.             MsgBox("Success!")
  35.         Else
  36.             tempCookies = Nothing
  37.             MsgBox("Failed.")
  38.         End If
  39.     End Sub
  40.  
  41.     Private Function getToken()
  42.         Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.twitter.co.uk")
  43.         Dim re As HttpWebResponse = r.GetResponse()
  44.         Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
  45.         Return GetBetween(src, "<input type=""hidden"" name=""authenticity_token"" value=""", """")
  46.     End Function
  47. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment