In this article I will explain with an example, how to do integration of Twitter API in ASP.Net website using C# and VB.Net.
The Twitter API will be integrated in ASP.Net website with the help of ASPSnippets Twitter API in C# and VB.Net.
Note: You can download the latest ASPSnippets.TwitterAPI.dll clicking the download link below.
Download DLL file
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.
You might also like to read:
HTML Markup
The HTML Markup consist of a Login Button and an HTML Table with the fields to populate the Twitter User Account and Profile details such as Screen Name, Email Address, Twitter Id, etc. of the Logged in User as well as some other User whose Twitter Id or Handle is being passed.
<asp:Button ID="btnLogin" runat="server" Text="Login with Twitter" OnClick="btnLogin_Click" />
<hr />
<table runat="server" id="tblTwitter" visible="false">
<tr>
<td colspan="2"><u>Logged in Twitter User's Profile</u></td>
</tr>
<tr>
<td style="width: 100px">Profile Image</td>
<td><asp:Image ID="imgProfile" runat="server" /></td>
</tr>
<tr>
<td>Name</td>
<td><asp:Label ID="lblName" runat="server" /></td>
</tr>
<tr>
<td>Twitter Id</td>
<td><asp:Label ID="lblTwitterId" runat="server" /></td>
</tr>
<tr>
<td>Screen Name</td>
<td><asp:Label ID="lblScreenName" runat="server" /></td>
</tr>
<tr>
<td>Description</td>
<td><asp:Label ID="lblDescription" runat="server" /></td>
</tr>
<tr>
<td>Tweets</td>
<td><asp:Label ID="lblTweets" runat="server" /></td>
</tr>
<tr>
<td>Followers</td>
<td><asp:Label ID="lblFollowers" runat="server" /></td>
</tr>
<tr>
<td>Friends</td>
<td><asp:Label ID="lblFriends" runat="server" /></td>
</tr>
<tr>
<td>Favorites</td>
<td><asp:Label ID="lblFavorites" runat="server" /></td>
</tr>
<tr>
<td>Location</td>
<td><asp:Label ID="lblLocation" runat="server" /></td>
</tr>
<tr>
<td>Email</td>
<td><asp:Label ID="lblEmail" runat="server" /></td>
</tr>
</table>
<hr />
<table runat="server" id="tblOtherTwitter" visible="false">
<tr>
<td colspan="2"><u>Other Twitter User's Profile</u></td>
</tr>
<tr>
<td style="width: 100px">Profile Image</td>
<td><asp:Image ID="imgOtherProfile" runat="server" /></td>
</tr>
<tr>
<td>Name</td>
<td><asp:Label ID="lblOtherName" runat="server" /></td>
</tr>
<tr>
<td>Twitter Id</td>
<td><asp:Label ID="lblOtherTwitterId" runat="server" /></td>
</tr>
<tr>
<td>Screen Name</td>
<td><asp:Label ID="lblOtherScreenName" runat="server" /></td>
</tr>
<tr>
<td>Description</td>
<td><asp:Label ID="lblOtherDescription" runat="server" /></td>
</tr>
<tr>
<td>Tweets</td>
<td><asp:Label ID="lblOtherTweets" runat="server" /></td>
</tr>
<tr>
<td>Followers</td>
<td><asp:Label ID="lblOtherFollowers" runat="server" /></td>
</tr>
<tr>
<td>Friends</td>
<td><asp:Label ID="lblOtherFriends" runat="server" /></td>
</tr>
<tr>
<td>Favorites</td>
<td><asp:Label ID="lblOtherFavorites" runat="server" /></td>
</tr>
<tr>
<td>Location</td>
<td><asp:Label ID="lblOtherLocation" runat="server" /></td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using ASPSnippets.TwitterAPI;
VB.Net
Imports System.Data
Imports ASPSnippets.TwitterAPI
Login with Twitter Account and fetch the Twitter Account and Profile Details
When the Login Button is clicked, the Authorization process starts and the User is redirected to Twitter Authorization page when he has to grant permission to the App for accessing the Profile details such as Screen Name, Email Address, Twitter Id, etc.
Once the User granted permission, he is redirect back to the Page along with the Access Token.
Inside the Page Load event, the Access Token is used to access the Twitter OAUTH API and the Twitter Profile details are fetched using the FetchProfile method.
Finally the Twitter Profile details are returned using a DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
TwitterConnect.API_Key = "<Twitter API Key>";
TwitterConnect.API_Secret = "<Twitter API Secret>";
if (!this.IsPostBack)
{
if (TwitterConnect.IsAuthorized)
{
TwitterConnect twitter = new TwitterConnect();
//LoggedIn User Twitter Profile Details
DataTable dt = twitter.FetchProfile();
imgProfile.ImageUrl = dt.Rows[0]["profile_image_url"].ToString();
lblName.Text = dt.Rows[0]["name"].ToString();
lblTwitterId.Text = dt.Rows[0]["Id"].ToString();
lblScreenName.Text = dt.Rows[0]["screen_name"].ToString();
lblDescription.Text = dt.Rows[0]["description"].ToString();
lblTweets.Text = dt.Rows[0]["statuses_count"].ToString();
lblFollowers.Text = dt.Rows[0]["followers_count"].ToString();
lblFriends.Text = dt.Rows[0]["friends_count"].ToString();
lblFavorites.Text = dt.Rows[0]["favourites_count"].ToString();
lblLocation.Text = dt.Rows[0]["location"].ToString();
lblEmail.Text = dt.Rows[0]["email"].ToString();
tblTwitter.Visible = true;
//Any other User Twitter Profile Details. Here jQueryFAQs
dt = twitter.FetchProfile("jQueryFAQs");
imgOtherProfile.ImageUrl = dt.Rows[0]["profile_image_url"].ToString();
lblOtherName.Text = dt.Rows[0]["name"].ToString();
lblOtherTwitterId.Text = dt.Rows[0]["Id"].ToString();
lblOtherScreenName.Text = dt.Rows[0]["screen_name"].ToString();
lblOtherDescription.Text = dt.Rows[0]["description"].ToString();
lblOtherTweets.Text = dt.Rows[0]["statuses_count"].ToString();
lblOtherFollowers.Text = dt.Rows[0]["followers_count"].ToString();
lblOtherFriends.Text = dt.Rows[0]["friends_count"].ToString();
lblOtherFavorites.Text = dt.Rows[0]["favourites_count"].ToString();
lblOtherLocation.Text = dt.Rows[0]["location"].ToString();
tblOtherTwitter.Visible = true;
btnLogin.Enabled = false;
}
if (TwitterConnect.IsDenied)
{
ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('User has denied access.')", true);
}
}
}
protected void btnLogin_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 = "<Twitter API Key>"
TwitterConnect.API_Secret = "<Twitter API Secret>"
If Not Me.IsPostBack Then
If TwitterConnect.IsAuthorized Then
Dim twitter As New TwitterConnect()
'LoggedIn User Twitter Profile Details
Dim dt As DataTable = twitter.FetchProfile()
imgProfile.ImageUrl = dt.Rows(0)("profile_image_url").ToString()
lblName.Text = dt.Rows(0)("name").ToString()
lblTwitterId.Text = dt.Rows(0)("Id").ToString()
lblScreenName.Text = dt.Rows(0)("screen_name").ToString()
lblDescription.Text = dt.Rows(0)("description").ToString()
lblTweets.Text = dt.Rows(0)("statuses_count").ToString()
lblFollowers.Text = dt.Rows(0)("followers_count").ToString()
lblFriends.Text = dt.Rows(0)("friends_count").ToString()
lblFavorites.Text = dt.Rows(0)("favourites_count").ToString()
lblLocation.Text = dt.Rows(0)("location").ToString()
lblEmail.Text = dt.Rows(0)("email").ToString()
tblTwitter.Visible = True
'Any other User Twitter Profile Details. Here jQueryFAQs
dt = twitter.FetchProfile("jQueryFAQs")
imgOtherProfile.ImageUrl = dt.Rows(0)("profile_image_url").ToString()
lblOtherName.Text = dt.Rows(0)("name").ToString()
lblOtherTwitterId.Text = dt.Rows(0)("Id").ToString()
lblOtherScreenName.Text = dt.Rows(0)("screen_name").ToString()
lblOtherDescription.Text = dt.Rows(0)("description").ToString()
lblOtherTweets.Text = dt.Rows(0)("statuses_count").ToString()
lblOtherFollowers.Text = dt.Rows(0)("followers_count").ToString()
lblOtherFriends.Text = dt.Rows(0)("friends_count").ToString()
lblOtherFavorites.Text = dt.Rows(0)("favourites_count").ToString()
lblOtherLocation.Text = dt.Rows(0)("location").ToString()
tblOtherTwitter.Visible = True
btnLogin.Enabled = False
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 btnLogin_Click(sender As Object, e As EventArgs)
If Not TwitterConnect.IsAuthorized Then
Dim twitter As New TwitterConnect()
twitter.Authorize(Request.Url.AbsoluteUri.Split("?")(0))
End If
End Sub
Screenshots
Twitter App Permission page
Message displayed when User denies permission
Successful Twitter Login
Demo
Downloads