Introduction:
This article demonstrates how to fetch data from Twitter using LinqToTwitter.
Background:
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:
- private static void Main(string[] args)
- {
- Console.WriteLine("working on it....");
- var tweetList = GetTwitterFeeds();
- Console.WriteLine("Tweets Count "+tweetList.Count);
- var file = new System.IO.StreamWriter("D:\\TweetsList.txt",true);
- foreach (var item in tweetList)
- {
- file.WriteLine(item.CreatedAt);
- }
- file.Close();
- Console.WriteLine("Done! check your drive file has been created");
- Console.ReadLine();
- }
- public static List<Status> GetTwitterFeeds()
- {
- string screenname = "csharpcorner";
-
- var auth = new SingleUserAuthorizer
- {
-
- CredentialStore = new InMemoryCredentialStore()
- {
-
- ConsumerKey = ConfigurationManager.AppSettings["consumerkey"],
- ConsumerSecret = ConfigurationManager.AppSettings["consumersecret"],
- OAuthToken = ConfigurationManager.AppSettings["accessToken"],
- OAuthTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
-
- }
-
- };
- var twitterCtx = new TwitterContext(auth);
- var ownTweets = new List<Status>();
-
- ulong maxId = 0;
- bool flag = true;
- var statusResponse = new List<Status>();
- statusResponse = (from tweet in twitterCtx.Status
- where tweet.Type == StatusType.User
- && tweet.ScreenName == screenname
- && tweet.Count == 200
- select tweet).ToList();
-
- if (statusResponse.Count > 0)
- {
- maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;
- ownTweets.AddRange(statusResponse);
-
- }
- do
- {
- int rateLimitStatus = twitterCtx.RateLimitRemaining;
- if (rateLimitStatus != 0)
- {
-
- statusResponse = (from tweet in twitterCtx.Status
- where tweet.Type == StatusType.User
- && tweet.ScreenName == screenname
- && tweet.MaxID == maxId
- && tweet.Count == 200
- select tweet).ToList();
-
- if (statusResponse.Count != 0)
- {
- maxId = ulong.Parse(statusResponse.Last().StatusID.ToString()) - 1;
- ownTweets.AddRange(statusResponse);
- }
- else
- {
- flag = false;
- }
- }
- else
- {
- flag = false;
- }
- } while (flag);
-
- return ownTweets;
- }
-
- }
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.