.NET Zone is brought to you in partnership with:

Sarfraz has posted 5 posts at DZone. You can read more from them at their website. View Full User Profile

Using Twitter via .NET - Beginners Guide

04.09.2013
| 3167 views |
  • submit to reddit

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 “http://127.0.0.1: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“/>

Namespace

Add following namespace in your code file.

using LinqToTwitter;

Code

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)

{

auth.CompleteAuthorization(Request.Url);

}

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

string.IsNullOrWhiteSpace(credentials.ConsumerSecret))

{

//Do nothing

}

else if (auth.IsAuthorized)

{

//Do nothing

}

else

{

auth.BeginAuthorization(Request.Url);

 if (!auth.IsAuthorized)

{

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

auth.BeginAuthorization(specialUri);

}

}

 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)

.SingleOrDefault();

gvTweetView.DataSource = searchResult.Statuses;

gvTweetView.DataBind();

#endregion

 #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>”;

#endregion

}

}

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.

Published at DZone with permission of its author, Sarfraz Khan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)