Create a twitter app
Visit http://dev.twitter.com, login and
create a twitter application. Application details- Name - your application name
- Description - your application description
- Website - your application's publicly accessible homepage. <<Put your link>>
- CallbackURl - leave blank /empty
Modify the Application Settings
Go to the Settings tab and change the application settings to
Read and Write. In English – Your app will get/pull stuff and also set/push stuff on
twitter
Copy your Applications Consumer Key, Consumer Secret, Token Secret, Token Key in a document, sticky
note… These keys would be used in da codes.
Design a VB web app
Create a new ASP.NET Website (Visual Basic). Design the Default.aspx page to
look like this.
In bits
- It’s made up of 2 panels (panel1 and panel2)
- Panel2 (Handles authorization and login)
- authorizationUrl is a label which displays the generated authenticating url
- pinTextBox would receive users input pin
- loginStatus displays responses when a user tries to login
- Panel1is my "mini TweetDeck"
- Username displays the screen name of the logged in twitter user/account.
- twitterResponse notifies the user whenever a tweet has been successfully posted.
- Status is a texbox for users to compose tweets
- tweetBtn updates your twitter account via the application name..
Code it
Note: These codes are in conformity with my ugly designs…
Get Twiterizer a Twitter oAuth library https://github.com/Twitterizer/Twitterizer but you only need is this folder (lib) http://www.mediafire.com/?46h6n8g2q6xtror which contains all plugins to be referenced.
Download, unzip and add reference to your .NET web application,project or website.
How to Add Reference in .NET Visual Studio 2010
- Right-Click your WebApplication in the Solutions Explorer
- select “Add Reference”
- Choose the Browse tab and locate the unzipped (lib) folder, select all items in the folder and click Ok
Include/import libraries in our Default.aspx.vb
Imports Twitterizer
Imports Newtonsoft
Declare variables that would only be accessible by methods in the class
it was initialized.
Wrap up! This is what your code should like..
its a simple application with crazy codes that makes you feel like a twitter-god. It would be awkward to debug every time you want to tweet via your application, save yourself the stress by
- Tokens – a simple collection security keys used by twitter apps for twitter interactions.
- consumerKey, consumerSecret, AccessToken, AccesTokenSecret – string of keys
Private tokens As New OAuthTokens()
Private consumerKey As String
Private consumerSecret As String
Private AccessToken As String
Private AccessTokenSecret As String
Page_Load is where you assign your keys and
trigger a hidden button to generate an authorization url.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
consumerKey = "YOUR CONSUMER KEY"
consumerSecret = "YOUR CONSUMER SECRET"
AccessToken = "YOUR ACCESS TOKEN"
AccessTokenSecret = "YOUR ACCESS TOKEN SECRET"
'Insert security tokens to perform authorized action on Twitter API
tokens.AccessToken = AccessToken
tokens.AccessTokenSecret = AccessTokenSecret
tokens.ConsumerKey = consumerKey
tokens.ConsumerSecret = consumerSecret
'A hidden button that generates auth.urls only if there's no postback
If Not Page.IsPostBack Then
hiddenButton_Click(sender, e)
End If
End Sub
Protected Sub hiddenButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles hiddenButton.Click
Dim authorizationTokens As OAuthTokenResponse = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, "oob")
authorizationUrl.Text = String.Format("http://twitter.com/oauth/authorize?oauth_token={0}", authorizationTokens.Token)
hiddenTextBox.Text = authorizationTokens.Token
End Sub
Login Button code handles when the user clicks the login button. The app gets the pin, tries to validate
and if successful it switches panel to allow authorized user to tweet..
Protected Sub loginBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles loginBtn.Click
Dim pin As String
pin = pinTextBox.Text
Dim accessTokens As OAuthTokenResponse
Try
accessTokens = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, hiddenTextBox.Text, pin)
userName.Text = "Hi " & accessTokens.ScreenName
Panel2.Visible = "False"
Panel1.Visible = "True"
Catch ex As Exception
loginStatus.Text = "Login failed - " & ex.Message
End Try
End Sub
Tweet Button code handles when an authorized user clicks the tweet button. the twitter
status of authorized user should be updated with the current text in status
textbox via my application..(tokens)
Protected Sub tweetBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tweetBtn.Click
Dim tweetResponse As TwitterResponse(Of TwitterStatus)
tweetResponse = TwitterStatus.Update(tokens, status.Text)
twitterResponse.Text = tweetResponse.Result.ToString()
If twitterResponse.Text = "Success" Then
status.Text = ""
End If
End Sub
Wrap up! This is what your code should like..
1: Imports Twitterizer
2: Imports Newtonsoft
3:
4: Public Class _Default
5: Inherits System.Web.UI.Page
6:
7: Private tokens As New OAuthTokens()
8:
9: Private consumerKey As String
10: Private consumerSecret As String
11: Private AccessToken As String
12: Private AccessTokenSecret As String
13:
14:
15: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
16: consumerKey = "CONSUMER KEY"
17: consumerSecret = "CONSUMER SECRET"
18: AccessToken = "ACCESS TOKEN"
19: AccessTokenSecret = "ACCESS TOKEN SECRET"
20:
21: 'Insert security tokens to perform authorized action on Twitter API
22: tokens.AccessToken = AccessToken
23: tokens.AccessTokenSecret = AccessTokenSecret
24: tokens.ConsumerKey = consumerKey
25: tokens.ConsumerSecret = consumerSecret
26:
27: 'A hidden button that generates auth.urls only if there's no postback
28: If Not Page.IsPostBack Then
29: hiddenButton_Click(sender, e)
30: End If
31:
32: End Sub
33: #Region "Authentication"
34: Protected Sub hiddenButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles hiddenButton.Click
35: Dim authorizationTokens As OAuthTokenResponse = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, "oob")
36:
37: authorizationUrl.Text = String.Format("http://twitter.com/oauth/authorize?oauth_token={0}", authorizationTokens.Token)
38: hiddenTextBox.Text = authorizationTokens.Token
39: End Sub
40:
41: Protected Sub loginBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles loginBtn.Click
42: Dim pin As String
43: pin = pinTextBox.Text
44:
45: Dim accessTokens As OAuthTokenResponse
46: Try
47: accessTokens = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, hiddenTextBox.Text, pin)
48: userName.Text = "Hi " & accessTokens.ScreenName
49: Panel2.Visible = "False"
50: Panel1.Visible = "true"
51: Catch ex As Exception
52: loginStatus.Text = "Login failed - " & ex.Message
53: End Try
54: End Sub
55:
56: #End Region
57:
58: #Region "Tweet"
59: Protected Sub tweetBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tweetBtn.Click
60: Dim tweetResponse As TwitterResponse(Of TwitterStatus)
61:
62: tweetResponse = TwitterStatus.Update(tokens, status.Text)
63: twitterResponse.Text = tweetResponse.Result.ToString()
64: If twitterResponse.Text = "Success" Then
65: status.Text = ""
66: End If
67: End Sub
68: #End Region
69:
70: End Class
Debug and start tweeting
debug any errors encountered and viola your app should be runninggg...its a simple application with crazy codes that makes you feel like a twitter-god. It would be awkward to debug every time you want to tweet via your application, save yourself the stress by