7

Hi does anyone know how to use the streaming API for C#? Therefore, whenever there is a new tweet in my account, it will be reflected in my program.

weikang
  • 113
  • 1
  • 2
  • 7

4 Answers4

11

So far the only reliable wrapper I've found for this in .Net land is TweetInvi. Try to ignore that the web site looks like it was designed by a hyperactive 10-year old (thanks MS 'metro' team), the actual library is very well designed and rock solid.

Assuming of course you have the relevant access tokens (if not see http://dev.twitter.com), an example of how easy it is to have up and running:

TwitterCredentials.SetCredentials(userToken,userTokenPrivate,apiKey,apiKeyPrivate);
_userStream = Stream.CreateUserStream();
_userStream.TweetCreatedByFriend += (sender,args) => Console.WriteLine(args.Tweet.Text);
_userStream.Start();

This will write the body of tweets to your console output, and it updates even faster than leaving the actual Twitter web site open. There are other events exposed for when a tweet is favourited, retweeted, when you have a new follower etc.

I can vouch for this library as being reliable - I am using it for my CovertTweeter project and have had absolutely no issues with it. In fact accessing the streaming API through TweetInvi has been even easier than the many brick walls I was left hitting when using REST wrappers like Linq2Twitter and TweetSharp.

nathanchere
  • 7,540
  • 14
  • 60
  • 84
5

Have a look at this post:

Streaming with New .NET HttpClient and HttpCompletionOption.ResponseHeadersRead

You don't have the complete implementation there but you will get the idea.

tugberk
  • 54,046
  • 58
  • 232
  • 321
3

Here is a sample which "Reads data from the Twitter Streaming API and adds it to MSMQ. A second process (included) reads from the queue, parses the json message, and updates a data store."

https://github.com/swhitley/TwitterStreamClient

You can change the above problem to generate an event when it updates the data store. In your program you can subscribe this event to do whatever you want.

If you are looking for OAuth based sample then please use "AuthPack" which Provides .NET oAuth for Twitter, Facebook, LinkedIn, and Google:

https://github.com/swhitley/AuthPack/tree/master/AuthPack

AvkashChauhan
  • 20,192
  • 3
  • 31
  • 64
  • Thanks for the quick reply, but the TwitterStreamClient is not using OAUTH2 to connect and I keep getting <401> unauthorized message from the server. – weikang Jun 08 '12 at 06:54
  • and this one: http://stackoverflow.com/questions/9847494/oauth-twitter-401-unauthorised – AvkashChauhan Jun 08 '12 at 07:08
0

I have found a good sample code that uses streaming API, here Twitterizer.

weikang
  • 113
  • 1
  • 2
  • 7
  • 1
    Actually the right url is now: https://github.com/Twitterizer/Twitterizer and the project seems abandoned right now, although code is there to get as is. – manudea May 14 '13 at 08:49