Our products, Nanode, wireless, MCUs, electronics and more

A desktop C# client using Twitterizer2

Posted: December 10th, 2010 | Author: | Filed under: Uncategorized | Tags: , , | 11 Comments »

Tutorial level: Beginner.

I did not see a simple example of using Twitterizer2 in a desktop app. For example, the Twitterizer app notes that desktop apps are not supported not being actively maintained, and in the forums Ricky notes “I am not maintaining a desktop example.” so I figured it might be useful to post one.

My guess is that there are not many times you would need a desktop app, so that makes sense. However, my project reads serial input from an Arduino and posts that, so a desktop app seemed appropriate.  I just want something running in the background. If it tweets, great, but my application did not require  connectivity. Putting in a dependency on being connected by hosting the app online did not seem appropriate.

Here are absolute basics of connecting using Twitterizer2 and oAuth from the desktop. I’m using the free version of MS Visual Studio 2010 and used Twitterizer2 2.3.1.

High Level View: Here are the essential steps.

  • Prerequisites – download etc.
  • Create a windows forms app.
  • Add a button to login with.
  • Create a token (you can’t tweet with it tho’).
  • Get permission from Twitter for the app using a second form for the pin.
  • Create a second token using the pin (this one works to tweet).
  • Add a text box or two. I used 2, one to output status and a second one for the user to enter what he wants to tweet.
  • Add a button to tweet.
  • Tweet.

That’s it!

Before you start there are two prerequisite steps:

  • Register your app with Twitter. You will get a consumer key and secret.
  • Download the Twitterizer2 files.

Next, create a new Windows forms project.

  • Put the Twitterizer2 files Newtownsoft.Json.dll and Twitterizer2.dll in your bin/debug directory.
  • In your Solution Explorer, right click on the References and add Twitterizer2 as a reference.
  • You’ll also need to add a Using Twitterizer2 directive to the top of your file.

We’ll be using the C# application settings to store our consumer key, secret and pin. If you aren’t familar with settings, C# provides a nifty way to store Key:Value pairs which your app needs. Right click on your project, select Properties. On the settings tab, add the three key:value pairs for consumerKey, consumerSecret and pin. Paste in the values you got from registering your app with Twitter. Your pin will be blank at this point – the user needs to get this from the Twitter website.

Now add textbox and a couple of buttons to the form.It might look something like this:


At the top of the form file, add this:

string consumerKey = Properties.Settings.Default.consumerKey;
string consumerSecret = Properties.Settings.Default.consumerSecret;
string callbackAddy = "oob";
OAuthTokenResponse tokenResponse2 = new OAuthTokenResponse();

Now we are going to code the button for getting the pin. The code below does not check if you already a valid user – you really want to do this, but I’m keeping it bare bones.

You also need a second form, called Enter_PIN. We’ll instantiate this form and wait for the user to paste in the pin they get from Twitter when they grant permission to your app. This permission is valid until the user revokes it. For the first (login) button add the following code:

private void button1_Click(object sender, EventArgs e)
 {
 // Need to check if the user is a valid user.

 OAuthTokenResponse tokenResponse = new OAuthTokenResponse();
 tokenResponse = Twitterizer.OAuthUtility.GetRequestToken(consumerKey, consumerSecret, callbackAddy);
 textBox1.Text = "Token is:  " + tokenResponse.Token.ToString();

 string target = "http://twitter.com/oauth/authorize?oauth_token="+tokenResponse.Token;
 try
 {
 System.Diagnostics.Process.Start(target);
 }
 catch (System.ComponentModel.Win32Exception noBrowser)
 {
 if (noBrowser.ErrorCode == -2147467259)
 MessageBox.Show(noBrowser.Message);
 }
    catch (System.Exception other)
 {
    MessageBox.Show(other.Message);
 }

 string pin = "xxxxxxx"; //This WILL NOT WORK. User needs to enter the PIN
 Enter_PIN enterpin = new Enter_PIN();
 enterpin.ShowDialog(); //show dialog causes it to wait for user input. Show() would not work
 pin = Properties.Settings.Default.pin;
 tokenResponse2 = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, tokenResponse.Token, pin);
 textBox1.Text = "App "+tokenResponse2.ScreenName.ToString()+" access allowed.";
 }

The second form needs a done button, and a text box for the user to enter input, so add a button and a textbox to the form. It might look something like this:

We’re not passing this pin as a parameter. Instead, we use Settings to store it.

private void button1_Click(object sender, EventArgs e)
{
    Properties.Settings.Default.pin = textBox1.Text;
    Properties.Settings.Default.Save();
    Close();
}

That’s pretty simple. OK, now that we have the pin we can construct a token which will allow us to tweet. Back in Form1 we need to add some code to our “tweet” button. This code constructs a token we can use to tweet, then reads the text box and tweets whatever is in the text box. You probably want to add some more things, for example code to change the text or otherwise make it obvious to the user that the text was tweeted.

private void button2_Click(object sender, EventArgs e)
 {
 OAuthTokens tokens = new OAuthTokens();
 tokens.AccessToken = tokenResponse2.Token;
 tokens.AccessTokenSecret = tokenResponse2.TokenSecret;
 tokens.ConsumerKey = consumerKey;
 tokens.ConsumerSecret = consumerSecret;

 //TwitterStatus tweetResponse = new TwitterStatus();
 TwitterResponse<TwitterStatus> tweetResponse = TwitterStatus.Update(tokens, textBox2.Text);
 if (tweetResponse.Result == RequestResult.Success)
 {
    textBox2.AppendText(" Update Successful.");
 }
 else
 {
    textBox2.AppendText(" Update did not go through.");
 }
 }

And that’s it! If all went well, you just tweeted. As a final comment, I just want to emphasize again how basic this is. If you want to do more, you need to add some stuff, like checking if the user is already logged in. This is just the absolute bare bones.  I hope it helps you.
\
UPDATE: Here is some code. Form1


11 Comments on “A desktop C# client using Twitterizer2”

  1. 1 Ricky said at 12:08 am on December 11th, 2010:

    It looks good, except that “For example, the Twitterizer app notes that desktop apps are not supported” is a little misleading. Nowhere does the site, notes, documentation, or forums say that desktop applications aren’t supported, only that I’m no longer writing and maintaining a desktop example application. I do support others developing desktop application.

  2. 2 admin said at 12:15 am on December 11th, 2010:

    You are right. I’ll update the post.

  3. 3 How to do the reply using Twitterizer dll said at 10:49 am on January 22nd, 2011:

    How do I relpy to a tweet using the dll, I can see Direct Message but cannot seem to see a Reply function using a twitterizer dll…Please help

  4. 4 M said at 3:07 am on February 10th, 2011:

    Can you include the source? I’m having trouble getting the code to work. With some modification I can get an application to run but nothing within the window itself appears/runs.

    Other than that, great tutorial!

  5. 5 admin said at 6:49 pm on March 28th, 2011:

    Hi,

    Sorry this took so long. I am not sure I have the source anymore. I’ll poke around for it.

  6. 6 Ryan Smyth said at 10:08 am on April 29th, 2011:

    Any chance you found the sample? It would be nice to quickly look at it rather than create a new project and type it all in. (Thanks for the article though!)

  7. 7 admin said at 12:27 pm on April 29th, 2011:

    @Ryan:

    I have added the code for form1 to the post as a zip file. Hope it helps you.

  8. 8 anthony said at 8:25 pm on May 4th, 2011:

    is there a way to retrieve the timeline of an account using twitterizer.dll

  9. 9 Kelvin said at 6:06 pm on August 1st, 2011:

    Thank you so much!
    This is the best tutorial ever!!!
    Very thorough
    After spending days trying twitterizer examples, this is the easiest one to follow!

  10. 10 Kelvin said at 6:32 pm on August 1st, 2011:

    Note, a possible typo
    I think it should be
    using Twitterizer;
    not
    using Twitterizer2;

    I have tried the latter but it doesn’t work.

    Just fyi.

  11. 11 Oscar said at 4:24 pm on August 22nd, 2012:

    Please, could you put a source code in visual basic.net ?

    Form1.cs


Leave a Reply

  • *