How to Create a Program to Login to Twitter in Visual Basic
Submitted by Yorkiebar on Monday, October 7, 2013 - 06:10.
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...
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...
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.
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!
Project Complete!
Below is the full source code and download to the project files.
- Imports System.IO
- Imports System.Text
- Imports System.Net
- Imports System.Text.RegularExpressions
- Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
- Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
- End Function
- Private Function getToken()
- Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.twitter.co.uk")
- Dim re As HttpWebResponse = r.GetResponse()
- Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
- Return GetBetween(src, "<input type=""hidden"" name=""authenticity_token"" value=""", """")
- End Function
- Dim loginCookies As CookieContainer
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim token As String = getToken()
- 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
- Dim tempCookies As New CookieContainer
- Dim encoding As New UTF8Encoding
- Dim byteData As Byte() = encoding.GetBytes(postData)
- Dim r As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://twitter.com/sessions"), HttpWebRequest)
- r.KeepAlive = True
- r.Method = "POST"
- r.CookieContainer = tempCookies
- r.ContentType = "application/x-www-form-urlencoded"
- r.Referer = "https://twitter.com/sessions"
- r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
- r.ContentLength = byteData.Length
- Dim re As Stream = r.GetRequestStream()
- re.Write(byteData, 0, byteData.Length)
- re.Close()
- Dim res As HttpWebResponse = DirectCast(r.GetResponse(), HttpWebResponse)
- tempCookies.Add(res.Cookies)
- loginCookies = tempCookies
- Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
- End Sub
- If (src.Contains("View my profile page")) Then
- loginCookies = tempCookies
- MsgBox("Success!")
- Else
- tempCookies = Nothing
- MsgBox("Failed.")
- End If
- Imports System.IO
- Imports System.Text
- Imports System.Net
- Imports System.Text.RegularExpressions
- Public Class Form1
- Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
- Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
- End Function
- Dim loginCookies As CookieContainer
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim token As String = getToken()
- 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
- Dim tempCookies As New CookieContainer
- Dim encoding As New UTF8Encoding
- Dim byteData As Byte() = encoding.GetBytes(postData)
- Dim r As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://twitter.com/sessions"), HttpWebRequest)
- r.KeepAlive = True
- r.Method = "POST"
- r.CookieContainer = tempCookies
- r.ContentType = "application/x-www-form-urlencoded"
- r.Referer = "https://twitter.com/sessions"
- r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
- r.ContentLength = byteData.Length
- Dim re As Stream = r.GetRequestStream()
- re.Write(byteData, 0, byteData.Length)
- re.Close()
- Dim res As HttpWebResponse = DirectCast(r.GetResponse(), HttpWebResponse)
- tempCookies.Add(res.Cookies)
- loginCookies = tempCookies
- Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
- If (src.Contains("View my profile page")) Then
- loginCookies = tempCookies
- MsgBox("Success!")
- Else
- tempCookies = Nothing
- MsgBox("Failed.")
- End If
- End Sub
- Private Function getToken()
- Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.twitter.co.uk")
- Dim re As HttpWebResponse = r.GetResponse()
- Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
- Return GetBetween(src, "<input type=""hidden"" name=""authenticity_token"" value=""", """")
- End Function
- 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
- 361 views