LINQ to Twitter Library using ASP.NET


In the last article, we have discussed, how to create and setup a twitter application. In this post, let’s discuss about how to use LINQ to twitter library using C# ASP.NET.

About LINQ to Twitter:

“LINQ to Twitter is an open source 3rd party LINQ Provider for the Twitter micro-blogging service. It uses standard LINQ syntax for queries and includes method calls for changes via the Twitter API.”

Let’s discuss an example which uses LINQ to Twitter library.

1. Create a new ASP.NET empty web application. We call it as “LinqtoTwitterExample”.

2. Download LINQ to Twitter library from here. Since we are using .NET framework 4.5, add a reference to “LinqToTwitter.dll” file from net45 folder.

3. Add the 5 keys in the web.config file namely “TwitterConsumerKey”, “TwitterConsumerSecret”, “TwitterAccessToken”, “TwitterAccessTokenSecret” and “TwitterScreenName” (it will hold your twitter screen name).


<appSettings>
 <add key="TwitterConsumerKey" value=""/>
 <add key="TwitterConsumerSecret" value=""/>
 <add key="TwitterAccessToken" value=""/>
 <add key="TwitterAccessTokenSecret" value=""/>
 <add key="TwitterScreenName" value="deshpandesuraj"/>
 </appSettings>

You can get the settings from the Twitter App.

4. Add a new aspx page call it as “Default.aspx”.

5. Add a button and a textbox on “Default.aspx” page. Set the max length of textbox to 140 since twitter uses only 140 characters.

In the code behind add reference to namespace: using LinqToTwitter;


var auth = new SingleUserAuthorizer
 {
 Credentials = new InMemoryCredentials
 {
 ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
 ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
 OAuthToken = ConfigurationManager.AppSettings["TwitterAccessToken"],
 AccessToken = ConfigurationManager.AppSettings["TwitterAccessTokenSecret"]
 }
 };

var twitterCtx = new LinqToTwitter.TwitterContext(auth);
 if (twitterCtx != null)
 {
 twitterCtx.UpdateStatus(textbox1.Text.Trim());
 }

6. Add another button and a listbox on “Default.aspx” page to hold the list of tweets.


var auth = new SingleUserAuthorizer
 {
 Credentials = new InMemoryCredentials
 {
 ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
 ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
 OAuthToken = ConfigurationManager.AppSettings["TwitterAccessToken"],
 AccessToken = ConfigurationManager.AppSettings["TwitterAccessTokenSecret"]
 }
 };

var twitterCtx = new LinqToTwitter.TwitterContext(auth);

var tweets = from t in twitterCtx.Status
 where t.Type == StatusType.User
 select t;

if (tweets != null)
 {
 foreach (var tweetStatus in tweets)
 {
 if (tweetStatus != null)
 {
 listbox1.Items.Add(new ListItem(tweetStatus.Text, tweetStatus.ID));
 }
 }
 }

Please Note: Your twitter application name will appear over tweet you sent.

Following are the commonly required twitter functions.

Get list of Followings:


List<string> followingsCollection = new List<string>();
 var auth = new SingleUserAuthorizer
 {
 Credentials = new InMemoryCredentials
 {
 ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
 ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
 OAuthToken = ConfigurationManager.AppSettings["TwitterAccessToken"],
 AccessToken = ConfigurationManager.AppSettings["TwitterAccessTokenSecret"]
 }
 };

var twitterCtx = new LinqToTwitter.TwitterContext(auth);

//cursor is used because twitter returns list partially
 string nextCursor = "-1";

while (!string.IsNullOrEmpty(nextCursor) && nextCursor != "0")
 {
 var followings =
 (from frndship in twitterCtx.Friendship
 where frndship.Type == FriendshipType.FriendsList &&
 frndship.ScreenName == ConfigurationManager.AppSettings["TwitterScreenName"] && frndship.Cursor == nextCursor
 select frndship)
 .SingleOrDefault();

if (followings != null)
 {
 foreach (var follower in followings.Users)
 {
 if (follower != null)
 {
 followingsCollection.Add(follower.Name);
 }
 }
 nextCursor = followings.CursorMovement.Next;
 }
 }
 return followingsCollection;

Get list of Followers:


List<string> followersCollection = new List<string>();

var auth = new SingleUserAuthorizer
 {
 Credentials = new InMemoryCredentials
 {
 ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
 ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
 OAuthToken = ConfigurationManager.AppSettings["TwitterAccessToken"],
 AccessToken = ConfigurationManager.AppSettings["TwitterAccessTokenSecret"]
 }
 };

var twitterCtx = new LinqToTwitter.TwitterContext(auth);

//cursor is used because twitter returns list partially
 string nextCursor = "-1";

while (!string.IsNullOrEmpty(nextCursor) && nextCursor != "0")
 {
 var followers =
 (from frndship in twitterCtx.Friendship
 where frndship.Type == FriendshipType.FollowersList &&
 frndship.ScreenName == ConfigurationManager.AppSettings["TwitterScreenName"] && frndship.Cursor == nextCursor
 select frndship)
 .SingleOrDefault();

if (followers != null)
 {
 foreach (var follower in followers.Users)
 {
 if (follower != null)
 {
 followersCollection.Add(follower.Name);
 }
 }
 nextCursor = followers.CursorMovement.Next;
 }
 }
 return followersCollection;

Get list of Favorites:


List<string> favoritesCollection = new List<string>();
 var auth = new SingleUserAuthorizer
 {
 Credentials = new InMemoryCredentials
 {
 ConsumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"],
 ConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"],
 OAuthToken = ConfigurationManager.AppSettings["TwitterAccessToken"],
 AccessToken = ConfigurationManager.AppSettings["TwitterAccessTokenSecret"]
 }
 };

var twitterCtx = new LinqToTwitter.TwitterContext(auth);

var favorites =
 (from fav in twitterCtx.Favorites
 where fav.Type == FavoritesType.Favorites &&
 fav.IncludeEntities == true
 select fav)
 .ToList();

if (favorites != null)
 {
 foreach (var fav in favorites)
 {
 if (fav != null)
 {
 favoritesCollection.Add(fav.Text);
 }
 }
 }
 return favoritesCollection;

Source Code Download:

Github [Repository Link]

Please note: For security point of view, I have removed twitter config key values from web.config file. If you are using my downloaded example, please add appropriate key values.

If you have any questions or doubts, please comment below.

Advertisements

2 thoughts on “LINQ to Twitter Library using ASP.NET

  1. Sagar Rawal

    Hi Thanks for this nice Tutorial…

    I am getting an Error. When i send Tweet or Get Tweet, I am receiving “JsonData instance doesn’t hold an int” Error. Any Idea regarding this ?? Also I can’t Post and event can’t Get Tweet.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s