Read Tweet From A Particular Hashtag In ASP.NET

Introduction

This article explains how to fetch data of a particular Hashtag from Twitter using TweetSharp API in ASP.NET.
 
Background
  1. VS2013
  2. C#
Before moving forward we have to create an app in Twitter using the following steps:
 
Step 1: Login to twitter and go to https://apps.twitter.com/ and you can see your apps if you have any, else click on Create New App button. You will be able to see application management window to create a new app. Here's the screenshot: 



I have filled the form as per my requirements. You can change this as per your.

Accept the agreement and click Create Your Twitter Application.

Step 2: 
Go to Keys and Access Tokens Tab and you will see Consumer Key (API Key) and Consumer Secret (API Secret). Here's the screenshoot.

 

Step 3: Now scroll down and click on Create my access token. You will see Access Token and Access Token Secret.
 
Step 4: I am showing data in console application.
  • Go to Visual Studio and create a console application.
  • Click Manage NuGet Packages.
  • Search for tweetsharp and install it as per the following image:


Step 5: Add the following code in your class with main method. Refer the comments for better understanding.
  1. public static string _consumerKey = "my key"// Your key
  2.         public static string _consumerSecret = "my key"// Your key  
  3.         public static string _accessToken = "my key"// Your key  
  4.         public static string _accessTokenSecret = "my key"// Your key  
  5.   
  6.         static void Main(string[] args)  
  7.         {  
  8.             TwitterService twitterService = new TwitterService(_consumerKey, _consumerSecret);  
  9.             twitterService.AuthenticateWith(_accessToken, _accessTokenSecret);  
  10.               
  11.             int tweetcount = 1;  
  12.             var tweets_search = twitterService.Search(new SearchOptions { Q = "#ItCanWait", Resulttype = TwitterSearchResultType.Popular });  
  13.             //Resulttype can be TwitterSearchResultType.Popular or TwitterSearchResultType.Mixed or TwitterSearchResultType.Recent  
  14.             List<TwitterStatus> resultList = new List<TwitterStatus>(tweets_search.Statuses);  
  15.             foreach (var tweet in tweets_search.Statuses)  
  16.             {  
  17.                 try  
  18.                 {  
  19.                     //tweet.User.ScreenName;  
  20.                     //tweet.User.Name;   
  21.                     //tweet.Text; // Tweet text  
  22.                     //tweet.RetweetCount; //No of retweet on twitter  
  23.                     //tweet.User.FavouritesCount; //No of Fav mark on twitter  
  24.                     //tweet.User.ProfileImageUrl; //Profile Image of Tweet  
  25.                     //tweet.CreatedDate; //For Tweet posted time  
  26.                     //"https://twitter.com/intent/retweet?tweet_id=" + tweet.Id;  //For Retweet  
  27.                     //"https://twitter.com/intent/tweet?in_reply_to=" + tweet.Id; //For Reply  
  28.                     //"https://twitter.com/intent/favorite?tweet_id=" + tweet.Id; //For Favorite  
  29.   
  30.                     //Above are the things we can also get using TweetSharp.  
  31.                     Console.WriteLine("Sr.No: " + tweetcount + "\n" + tweet.User.Name + "\n" + tweet.User.ScreenName + "\n" + "https://twitter.com/intent/retweet?tweet_id=" + tweet.Id);  
  32.                     tweetcount++;  
  33.                 }  
  34.                 catch { }  
  35.             }  
  36.             Console.ReadLine();  
  37.         } 
Now run the code you will see only 15 results.

You can change code line-12 as per the following code to get 100 results.
  1. var tweets_search = twitterService.Search(new SearchOptions { Q = "#ItCanWait", Resulttype = TwitterSearchResultType.Popular, Count = 100 }); 
 
Sometimes you may find the following error: Arithmetic operation resulted in an overflow because the count parameter has Integer value as per TweetSharp API and the Tweet Count is more than integer range in your hash tag.

To get over it remove Resulttype and Count.
 
Note:
  • #ItCanWait is my hashtag as per my code, you can change it as per your requirement.
  • Twitter API allows maximum 100 data at a time and 15 is the default.
  • Change the Keys as per your Twitter App.


Similar Articles