Over a million developers have joined DZone.

Using Twitter via .NET - Beginners Guide

DZone 's Guide to

Using Twitter via .NET - Beginners Guide

· ·
Free Resource

Objective :

In this post i will quickly explain how can we interact with Twitter API using .Net code.

Ingredients :

LinqToTwitter , Visual Studio 2010

Recipe :

You may find several popular Dlls on internet that communicate with Twitter. I have used LinqToTwitter & have found it easy to understand & implement.

You need to have “ twitterConsumerKey ” & “ twitterConsumerSecret ” that you will use from within your application. Get your applications twitter credentials from https://dev.twitter.com/apps

Our application URL will be like “http://localhost: PortNumber ” but once you are on Twitter Applications settings Form : It does not validate “http://localhost:8085" , therefore an alternate is to use “ PortNumber ” as our website placeholder. it will serve our purpose.

.NET code

Create a simple Asp.net web application in visual studio 2010.

Web.Config Changes

Add following in your web.config appSettings

<add key=”twitterConsumerKey” value=” twitterConsumerKey “/>

<add key=”twitterConsumerSecret” value=” twitterConsumerSecret “/>


Add following namespace in your code file.

using LinqToTwitter;


I used following code to explore Search feature & User Context.

private WebAuthorizer auth;

private TwitterContext twitterCtx;

 protected void Page_Load(object sender, EventArgs e)


IOAuthCredentials credentials = new SessionStateCredentials();

 if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)


credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];

credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];


 auth = new WebAuthorizer


Credentials = credentials,

PerformRedirect = authUrl => Response.Redirect(authUrl)


 if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)




 if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||



//Do nothing


else if (auth.IsAuthorized)


//Do nothing





 if (!auth.IsAuthorized)


Uri specialUri = new Uri(Request.Url.ToString());




 if (auth.IsAuthorized)


twitterCtx = new TwitterContext(auth);

 #region Search Text

var searchResult =

(from srch in twitterCtx.Search

where srch.Type == SearchType.Search &&

srch.Query == “Brian Adams”

select srch)


gvTweetView.DataSource = searchResult.Statuses;



 #region UserInfo

var users =

(from user in twitterCtx.User

where user.Type == UserType.Lookup &&

user.ScreenName == “prosarfraz”

select user);

 string strHtml = string.Empty;

 foreach (User objUser in users)


 strHtml += “<TR>”;

strHtml += “<TD> Screen Name </TD>”;

strHtml += “<TD>” + objUser.ScreenName + “</TD>”;

strHtml += “<TD> Favorites Count </TD>”;

strHtml += “<TD>” + objUser.FavoritesCount + “</TD>”;

strHtml += “<TD> Followers </TD>”;

strHtml += “<TD>” + objUser.FollowersCount + “</TD>”;

strHtml += “<TD> Description </TD>”;

strHtml += “<TD>” + objUser.Description + “</TD>”;

strHtml += “</TR>”;


ulsrc.InnerHtml = “<Table border =’1′>” + strHtml + “</Table>”;




Executing this code will redirect the web page user to twitter authentication page. One you authorize from your twitter account it will redirect you back to the application page to process #SearchResult & #UserInfo regions.

I hope this has provided you a quick insight on how to connect to Twitter Api using C# code.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}