In this article I will explain how to posts tweets or update twitter status message using ASP.Net with VB.Net and C#.
Create Twitter Application and get API Key and API Secret
You will need to create an application on
Twitter Apps and get an API Key and API Secret.
HTML Markup
The HTML Markup is very simple. There’s an ASP.Net Button btnAuthorize to authorize the Twitter user and get the access token, a multiline ASP.Net TextBox txtTweet to allow user type the status message to be published on the Twitter and finally another ASP.Net Button btnTweet to trigger the process.
<asp:Button ID="btnAuthorize" runat="server" Text="Authorize" OnClick="btnAuthorize_Click" CausesValidation = "false" />
<hr />
<asp:Panel runat="server" ID = "pnlTweet" Enabled = "false">
<asp:TextBox ID="txtTweet" runat="server" TextMode="MultiLine" Height="50"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtTweet"
ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btnTweet" runat="server" Text="Tweet" OnClick="btnTweet_Click" /></asp:Panel>
Namespaces
You will need to import the following Namespaces
C#
using ASPSnippets.TwitterAPI;
VB.Net
Imports ASPSnippets.TwitterAPI
Authorize the User using Twitter OAUTH API
Firstly we will need to authorize the user using Twitter API and get the Access Token so that we can tweet the status message on Twitter.
In the below code we are authorizing the user using Twitter and also asking him to provide permission to the application to tweet the status message on the user’s twitter account.
C#
protected void Page_Load(object sender, EventArgs e)
{
TwitterConnect.API_Key = "<Your Twitter API Key>";
TwitterConnect.API_Secret = "<Your Twitter API Secret>";
if (!IsPostBack)
{
if (TwitterConnect.IsAuthorized)
{
btnAuthorize.Enabled = false;
pnlTweet.Enabled = true;
}
if (TwitterConnect.IsDenied)
{
ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('User has denied access.')", true);
}
}
}
protected void btnAuthorize_Click(object sender, EventArgs e)
{
if (!TwitterConnect.IsAuthorized)
{
TwitterConnect twitter = new TwitterConnect();
twitter.Authorize(Request.Url.AbsoluteUri.Split('?')[0]);
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
TwitterConnect.API_Key = "<Your Twitter API Key>"
TwitterConnect.API_Secret = "<Your Twitter API Secret>"
If Not IsPostBack Then
If TwitterConnect.IsAuthorized Then
btnAuthorize.Enabled = False
pnlTweet.Enabled = True
End If
If TwitterConnect.IsDenied Then
ClientScript.RegisterStartupScript(Me.[GetType](), "key", "alert('User has denied access.')", True)
End If
End If
End Sub
Protected Sub btnAuthorize_Click(sender As Object, e As EventArgs)
If Not TwitterConnect.IsAuthorized Then
Dim twitter As New TwitterConnect()
twitter.Authorize(Request.Url.AbsoluteUri.Split("?"c)(0))
End If
End Sub
The below screenshot displays the Twitter Authorization page asking the user to provide permission to the application to update the status on user’s tweeter account
Tweet and update status on Twitter
Below is the code to update the status onto the user’s Twitter account.
C#
protected void btnTweet_Click(object sender, EventArgs e)
{
TwitterConnect twitter = new TwitterConnect();
twitter.Tweet(txtTweet.Text);
}
VB.Net
Protected Sub btnTweet_Click(sender As Object, e As EventArgs)
Dim twitter As New TwitterConnect()
twitter.Tweet(txtTweet.Text)
End Sub
Demo
Downloads