Read Tweets From Twitter Using LinqToTwitter

Introduction:
 
This article demonstrates how to fetch data from Twitter using LinqToTwitter.
 
Background:
  1. VS2013
  2. C#
  3. LinqToTwitter 
First we need to register an app in Twitter, for that here are the steps:
 
Step 1:  Login to twitter and go to https://dev.twitter.com/  and scroll down to bottom under Tools section, click on Manage your apps. You will be able to see the dashboard with listing of apps that are created before. Here's the screenshot:
 
 
 
 Step 2: Click Create New App and you will get the following screen. Now, fill the fields accordingly:
 
 
 
Step 3: Go to keys and Access tokens tab, inside that we will have Consumer key and Consumer Secret key and we need Access token and Access TokenSecret. To get Access keys, click on Generate Access Token button. Now Access token and Acess Token Secret will be visible. We need these 4 keys to fetch data from twitter.
 
Step 4: Now open VS 2013 and create console application (c#) and add LinqToTwitter library from nuget.
 
Step 5. Create Method as in the following code snippet:
  1. private static void Main(string[] args)  
  2. {  
  3.       Console.WriteLine("working on it....");  
  4.       var tweetList = GetTwitterFeeds();  
  5.       Console.WriteLine("Tweets Count "+tweetList.Count);  
  6.       var file = new System.IO.StreamWriter("D:\\TweetsList.txt",true); // Make sure to change the path according to your system  
  7.       foreach (var item in tweetList)  
  8.       {  
  9.           file.WriteLine(item.CreatedAt);  
  10.       }  
  11.       file.Close();  
  12.       Console.WriteLine("Done! check your drive file has been created");  
  13.       Console.ReadLine();  
  14. }  
  1. public static List<Status> GetTwitterFeeds()  
  2. {  
  3.     string screenname = "csharpcorner";  
  4.   
  5.     var auth = new SingleUserAuthorizer  
  6.     {  
  7.   
  8.         CredentialStore = new InMemoryCredentialStore()  
  9.         {  
  10.   
  11.             ConsumerKey = ConfigurationManager.AppSettings["consumerkey"],  
  12.             ConsumerSecret = ConfigurationManager.AppSettings["consumersecret"],  
  13.             OAuthToken = ConfigurationManager.AppSettings["accessToken"],  
  14.             OAuthTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]  
  15.   
  16.         }  
  17.   
  18.     };  
  19.     var twitterCtx = new TwitterContext(auth);  
  20.     var ownTweets = new List<Status>();  
  21.   
  22.     ulong maxId = 0;  
  23.     bool flag = true;  
  24.     var statusResponse = new List<Status>();  
  25.     statusResponse = (from tweet in twitterCtx.Status  
  26.     where tweet.Type == StatusType.User  
  27.     && tweet.ScreenName == screenname  
  28.     && tweet.Count == 200  
  29.     select tweet).ToList();  
  30.   
  31.     if (statusResponse.Count > 0)  
  32.     {  
  33.         maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;  
  34.         ownTweets.AddRange(statusResponse);  
  35.   
  36.     }  
  37.     do  
  38.     {  
  39.         int rateLimitStatus = twitterCtx.RateLimitRemaining;  
  40.         if (rateLimitStatus != 0)  
  41.         {  
  42.   
  43.             statusResponse = (from tweet in twitterCtx.Status  
  44.             where tweet.Type == StatusType.User  
  45.             && tweet.ScreenName == screenname  
  46.             && tweet.MaxID == maxId  
  47.             && tweet.Count == 200  
  48.             select tweet).ToList();  
  49.    
  50.             if (statusResponse.Count != 0)  
  51.             {  
  52.                 maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;  
  53.                 ownTweets.AddRange(statusResponse);  
  54.             }  
  55.             else  
  56.             {  
  57.                 flag = false;  
  58.             }  
  59.         }  
  60.         else  
  61.         {  
  62.             flag = false;  
  63.         }  
  64.         } while (flag);  
  65.   
  66.                 return ownTweets;  
  67.     }  
  68.   
  69. }   
Description: The LINQ to Twitter authorizers are designed to bypass the Twitter authorization process and just sign a request, if you have all the four keys.
 
Twitter API allows only 180 request per 15mins if it gets exceeded we will get RateLimit Exceeded Exception.
 
For twitter rate limit exceeding, check the following link: https://dev.twitter.com/rest/public/rate-limiting

Up Next
    Ebook Download
    View all
    Learn
    View all