.Net C# – Twitter API For Desktop

9 Comments

I made a post almost 3 months ago about a .Net Twitter API that I had done.  Since then, I have made some modifications to the API.  I have added a couple of more features.  I also included various classes from the System.Web namespace so that I wouldn’t need a reference to that namespace.  This allows the API to be used on a mobile device with a very small footprint(the System.Web.dll file is 5MB in size, my .dll is 52KB).

I am missing a few features from my API.  Most notably the new “Retweet” functionality and the “Lists” functionality.  I am also missing some of the account methods.  Other than that, I believe I have most, if not all, of the other methods.

This API supports both Basic Auth and OAuth.  I would advise to use OAuth, since Twitter will be depreciating Basic Auth in June 2010.  To learn more about how OAuth works, check out my other blog post where I try to explain OAuth.

Remember that this API is for the Desktop OAuth only.  Feel free to download it and make changes to allow OAuth from a web application(yes, they are different “workflows”).   Basic Auth should work for both Desktop and Web applications.

I give credit to Shannon Whitley for his original OAuth code.  I made some modifications to it, but the base code of the OAuth is his.

Here is a code snippet on using the API for OAuth authorization.

// creats instance and sets Consumer and ConsumerSecret values
TwitEclipseAPI twit = new TwitEclipseAPI();
twit.OAuthConsumerKey = "yourConsumerKey";
twit.OAuthConsumerSecret = "yourConsumerSecret";

// makes request to get unauthorized request token
// The method will concatenate the request token to Twitter's Desktop OAuth
//    url (http://twitter.com/oauth/authorize)
string url = twit.OAuthGetUnauthorizedRequestToken();

// opens the user's default browser and browses to Twitter's OAuth page
Process.Start(url);

//  You will need to get the PIN from the user
//  I just created a popup and have the user enter the PIN
//       into a textbox in the popup
frmPinPopup popup = new frmPinPopup();
popup.ShowDialog();

string PIN = popup.PIN;
popup.Dispose();

// Gets the Token and TokenSecret that will be needed for all
//   subsequent requests.  You will need to save these values to keep
//   from forcing the user to authorize your application everytime they
//   open your application.
twit.OAuthRequestAccessToken(PIN);

// You can check for success by checking the OAuthAccessToken
//    and OAuthAccessTokenSecret values.  If they are populated, then
//    it was successful.  If they are empty, then it failed.
if (!string.IsNullOrEmpty(twit.OAuthAccessToken) && !string.IsNullOrEmpty(twit.OAuthAccessTokenSecret))
{
    MessageBox.Show("Authorization Successful");
}

Each method of the API will check the OAuthAccessToken and OAuthAccessTokenSecret values to determine whether it needs to do the request using OAuth or Basic Auth.

I have put the code on CodePlex.  It will contain both the .dll file, and the source code.  I felt it was easier to keep up with using CodePlex.

If you have code questions, you can post here.  However, if you are on Google Wave, you can go here.  If you would like Google Wave, I have about 20 invitations that I can send out.  Using Google Wave would be much better since we could exchange code using a code snippet tool.

Tags: , , , ,

9 Responses to “C# – Twitter API For Desktop”

  1. Miquel Says:

    I’m using this code with the last realase on CodePlex and when I call:
    twit.OAuthRequestAccessToken(PIN);

    Twitter always response: 401 unauthorized…

    Can you help me?

    Thank you!

  2. Ryan Alford Says:

    @Miquel

    Can you send me an email to ryanalford843 at gmail dot com? It would probably be easier to go over your code through email and easier to converse than through the blog.

  3. algabba Says:

    hi will I downloaded the twitEclipseAPI but I can’t found any example how I can use it ..! do you have any OS application plz :( tnx .

  4. Ryan Alford Says:

    @Algabba

    I tried to make the API as self-explanatory as possible. I also have comments for most(if not all) the methods.

    What are you looking to do?

  5. Tyler Says:

    I need to contact you what is your email?

  6. Ryan Alford Says:

    my email is in the second comment.

  7. Nina@contentblogs Says:

    I know this if off topic but I’m looking into starting my own blog and was wondering what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100% certain. Any recommendations or advice would be greatly appreciated. Cheers

  8. wentworth kitchens Says:

    Hello, I’m 16 and job prospects where i am in Sunderland (UK) are terrible. So I’m trying to learn all about sites. How you can construct them, what a nice one really should look like and what it should include. I spend all my hours surfing the Net and reading all about net design and so on. I thought WordPress was some sort of a dictionary until recently !. What I dont get is how WordPress make any cash ?! Guess I’ll find out soon enough !!. So thanks for your website. Bookmarked and I will return to see any changes etc. Good luck and wish me some !

  9. puma soccer shoes Says:

    Could I just say nice to read a comfort to discover somebody who essentially understands exactly what they may be discussing on the internet. You certainly recognize how to bring a problem to light and allow it to important. Even more people require to read it all and comprehend this particular section of the story. I cant believe you aren’t very popular because you actually have the gift.

Leave a Reply