Coding the Tweet: Building a Custom Branded Twitter Application
Tuesday, April 21, 2009   

UPDATE: The Coding the Tweet demo application and source code have been updated to support PIN-based authorization through oAuth.

With millions of users and an ecosystem saturated with over 700 custom applications, Twitter's all the rage these days. Love it or hate it, Twitter has become a powerful medium for connecting with people and marketing your skills to a wider audience. As the saying goes:

Twitter marches on.

The popularity of the Twitter platform means that Twitter users have the luxury of choosing from an army of custom websites and desktop applications (my personal favorite is TweetDeck) allowing them to tweet, browse, and search Twitter in new and ever-so-trendy ways. And one of the clever but often-overlooked features of Twitter is that it upgrades these custom applications to first-class citizens within the Twitterverse by tagging each tweet with the hyperlinked name of the tool or medium used to create the tweet:

 

These attributions serve as a gentle encouragement for builders of Twitter tools, as well as a way to get the word out about the latest and greatest Twitter apps, and they even (in my opinion) help foster a sense of exclusivity. It's a little silly, but what can you do? Twitter psychology is a powerful and subtle thing.

Vanity Plates

So everybody has their favorite Twitter tool, and power-users tend to gravitate towards the more powerful and/or newer tools, and it's a win-win-win for all concerned: for Twitter, for Twitter users, and for the builders of Twitter tools. And as it turns out, it's actually pretty easy to build your own custom branded Twitter application in case you want to join in on the fun.

Registering a Custom Twitter Application

Building a bare-bones Twitter client will take you all of about an hour. First, sign into your Twitter account and register a new custom application. Here's mine:

Enter the details for your custom application, making sure to specify (for the purposes of this demo):

  • Application Type: Client
  • Default Access Type: Read & Write

Save those settings and you'll be presented with a page containing some authorization/authentication magic for your custom Twitter application. Make a note of your consumer key and consumer secret, as we'll be plugging those in shortly. (And it's okay if you have no idea what these are used for.)

At this point, you've registered your custom application with Twitter. The only problem is, your custom application doesn't actually exist yet. Let's fix that.

A Bare-Bones Twitter Client

You can build a custom Twitter app using just about any language in existence, as a desktop application or a web application. We'll implement ours as a desktop application using C# and .NET, but if you'd rather go another route, the Twitter API wiki contains tutorials and sample code in different languages.

Our bare-bones Twitter application will have the following functionality:

It will feature the simplest possible user interface: 

The first time you run the client, click the Configure button and cut-and-paste the Consumer Key and Consumer Secret you were given when you registered your custom application and hit the "Get PIN!" button.

Twitter application settings

Your default web browser will open and you'll see a message similar to the following.

Click the "Allow" button. Once you've done this, and you'll only have to do it once, you'll be given a PIN:

Enter your pin in the Coding the Tweet settings and click Authorize! 

You can then use the appication to post tweets tagged with your custom application name and hyperlink.

The Source Code

You can download the complete source code for the above "generic Twitter" application so you can take a look at what's going on under the hood. Once all the authentication and authorization stuff is out the way, sending a tweet is as simple as:

// URL-encode the tweet...
string tweet = HttpUtility.UrlEncode(txtTweet.Text);

// And send it off...
string xml = _oAuth.oAuthWebRequest(
    oAuthTwitter.Method.POST,
    "http://twitter.com/statuses/update.xml",
    "status="+tweet);

The authentication-related code is slightly more complex (but not much). Essentially, we're going to execute the following steps. These only have to be performed once:

  1. Accept the Consumer Key and Consumer Secret from the user.
  2. Ask Twitter for the URL of the "authorize this application" page on Twitter.
  3. Open the default web browser and navigate to that page.
  4. Allow the user to specify the PIN he was provided by Twitter.
  5. Bundle this PIN in the call to the http://twitter.com/oauth/access_token API.
  6. Store the returned access_token for future use.

That's really all there is to it. You now have a custom branded (if somewhat bare-bones) Twitter client which will tag each of your tweets with your URL of choice. The above code can easily be expanded to provide additional functionality or even turned into a full-fledged Twitter client. It can also be very easily converted to a web-based paradigm.

(Thanks to Shannon Witley and Eran Sandler for implementing the oAuth wrapper class to handle the sometimes-pesky details of oAuth authentication.)


Posted by James Devlin   49 comment(s)

Man I am loving this. I've ALWAYS wondered how they get those branded links in there. Will be trying this immediately. *Great* article.

Steve J. on 4/21/2009 6:55 AM (293 days ago)

I'll be smacking down with the 'Hammer of Unyielding Awesomeness' shortly. I put together a simple Twitter demo (using basic auth) a couple weeks ago. I actually came across the oAuth class but couldn't get it to work in a desktop scenario. I'll be interested to see how you did it.

(Assuming I can procrastinate at work this fine morning...uggh)

Anonymous on 4/21/2009 7:23 AM (293 days ago)

Nice effort, and well-written, but C# and .NET are terrible languages for this sort of thing. .NET is hardly even capable of doing this properly and in the marketplace you'll see that practically nobody uses C# for this sort of web interactivity. PHP/Ruby/etc. are the *only* way to do web-related stuff properly. This is becoming clear now that ASP.NET has such a puny market share compared to other languages.

EVH on 4/21/2009 8:14 AM (293 days ago)

Hmmm, isn't the info on how to do this easy to find? I suppose this post might help some people looking for a clear, step by step guide. The oAuth stuff is a little new for twitter.

I've been meaning to try and implement a twitter bot (non-spammy of course), in Python, half because I want to learn the language, and half because I want a crack at the twuring test - http://www.squidoo.com/twuring

ehsanul on 4/21/2009 9:17 AM (293 days ago)

I registered a twitter application and tried Coding the Tweet. Everything seems to work fine, but twitter shows the tweet as "from the web" even though I sent it using Coding the Tweet. Any ideas?

Daniel Stutzbach on 4/21/2009 1:59 PM (293 days ago)

Laughinganiel I tried the same thing. Did you register the application as a desktop app or a web app? And did you authorize the app.

JeremyX on 4/21/2009 4:42 PM (293 days ago)

"in the marketplace you'll see that practically nobody uses C# for this sort of web interactivity"

@EVH Uh... no.

news.netcraft.com/.../...09_web_server_survey.html

JeremyX on 4/21/2009 4:46 PM (293 days ago)

JeremyX: As a Desktop app, and, yes, I authorized it.

Daniel Stutzbach on 4/21/2009 5:13 PM (293 days ago)

Daniel, if this is just a test version of your twitter app, send me the consumer key/secret (you can regen these after) to james AT codingthewheel.com. Curious to know what the issue is.

James Devlin on 4/21/2009 5:43 PM (293 days ago)

>Hmmm, isn't the info on how to do this easy to find?

Yes.

Well, it's easier to find for other languages. I could only find a few examples for .NET/C# and none of them really went through all the steps. And I couldn't find a single working example of how to build a *desktop* Twitter app with C#.

James Devlin on 4/21/2009 6:12 PM (293 days ago)

@EVH: You're fired. Fired from this blog.

James Devlin on 4/21/2009 6:38 PM (293 days ago)

I totally see the point of this post. I'm going to take your source code and add a regular-expression Twitter search feature to it.

Maybe we can bootstrap this thing into an open-source Twitter app.

And I think EVH was trolling. Anybody with half a brain knows ASP.NET is quickly becoming the dominant web paradigm.

Ed K. on 4/21/2009 7:29 PM (292 days ago)

I just started using Twitter and I have to say I love it. It's a great way to update people on how I'm getting on during my poker... Even if it's going really badly.

Poker Forum on 4/26/2009 5:50 PM (288 days ago)

I'm not sure what changed, but everything works fine now. Twitter correctly shows my application name instead of "web".

Daniel Stutzbach on 4/28/2009 9:44 AM (286 days ago)

Does it have to be a desktop app to get the name to show up instead of "web"?

Morder on 4/30/2009 6:26 AM (284 days ago)

>Does it have to be a desktop app to get the name to show up instead of "web"?

No. The attribution should show regardless of whether it's a desktop or web app. It may take a while for it to percolate through the system. (Just make sure the custom application settings on Twitter correctly identify the type of application.)

The Twitter API Wiki has oAuth examples in PHP, Python, Ruby, and .NET, most or all of which are web-based.

James Devlin on 4/30/2009 9:38 AM (284 days ago)

I can verify that the application works. Quite simple. Ghanks for laying it all out. Not sure how I'm going to use this but I will use it. Can't believe there's not more .NET-related information for Twitter. That page you linked to which is also linked from the Twitter API wiki is very sparse.

DIY FTW! on 5/1/2009 7:31 AM (283 days ago)

Thanks for the application.

I have tried as well and it just says "web" when I submit a tweet. Then, suddenly, it began to work!

So, encouraged, I registered a new app and deleted the old one (which was for testing), and it hasn't worked since. I'm not saying it's your program - because it DID work - it must be something wrong on Twitter's end but I'm not sure what.

spankster on 5/2/2009 12:20 PM (282 days ago)

>So, encouraged, I registered a new app and deleted the old one (which was for testing), and it hasn't worked since.

@spankster, it looks like there's some latency between registering a custom application and having its updates appear with the proper attribution. Also, the custom app authorization page on the Twitter side is a little finicky. Did you reset all your settings (in the Coding the Tweet sample app) and cut-and-paste your new consumer key/secret? Were you able to authorize the new application through the Twitter authorization page? If yes, and provided we're not in one of those windows where the Twitter team is working the kinks out of oAuth or the custom app registration process, everything should work.

James Devlin on 5/2/2009 12:57 PM (282 days ago)

@James Devlin, Thanks. It took a long time before it started working. Smile

Morder on 5/11/2009 11:11 AM (273 days ago)

Hi! I've been studing your code, but if I use special characters its always fails with a HTTP 401... can you help?

Cristovao Morgado on 5/20/2009 4:05 PM (264 days ago)

Hi, when I enter my keys and save, the app crashes with 401 failure. Whats the reason?

Aditya on 5/29/2009 7:05 AM (255 days ago)

Aditya, I am getting the same error.

D. on 6/6/2009 5:08 PM (247 days ago)

thanks for this application. As far as I understand, Twitter just introduced a PIN based OAuth sytems, which means that the application doesn't work anymore. The user must be prompted to enter the 6digit pin. Did you consider implementing a solution for that?

Thomas on 6/14/2009 5:55 PM (239 days ago)

here are infos on the new rules for callbacks: groups.google.com/.../da10ad671877eaca

Eray Basar on 6/14/2009 6:02 PM (239 days ago)

it still works without the PIN need!
I've fixed a bug with GET requests.. I was trying to get mentions and found it:

public string WebRequest(Method method, string url, string postData)
{
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = "";
if (method == Method.GET)
url += "&" + postData;

Cristovao Morgado on 6/24/2009 8:06 AM (229 days ago)

Can anybody confirm if this works without the PIN? I dont think so...
@cristovao is this a fix for the pin-related "bug"?

denkmalschutz immobilien on 7/7/2009 8:46 AM (216 days ago)

Any update on the pin code thing leading to the 401 error?

Bokkie on 7/17/2009 6:36 PM (206 days ago)

The 401 error has been resolved. You need to bundle the PIN (specified by the user after being directed to Twitter for authorization) as the oauth_verifier parameter when you call the /oauth/access_token method. I'll post the updated code shortly~

James Devlin on 7/18/2009 1:35 AM (205 days ago)

Hi, does anyone know if you register a app name with twitter, are you able to change it later down the line without having to go through the whole process? would it be straight forward?. I have a twitter app at the moment and would like to use oauth so my current app name appears from tweets sent via it but i would be changing the name if a few weeks so not sure what i should do.

Any advice would be much appreciated.

Thanks
Adam

adam on 7/22/2009 8:02 AM (201 days ago)

Adam: Yes you can change the app name. Log into Twitter. Click 'Settings'. Go the the 'Connections' tab. In the sidebar click the 'Developers can edit the registration settings for their applications here.' Then click the name of your application. Then click the 'Edit Application Settings' button.

James Devlin on 7/22/2009 4:30 PM (201 days ago)

Hi James,

Thanks for your help, Much appreciated Smile

Adam

adam on 7/24/2009 7:08 AM (199 days ago)

Nice, all is working 100% !!! Congrats!

Cristovão on 7/28/2009 5:01 AM (195 days ago)

Anyone know how to list all the users who are using the app?

I have the OAuth working, and it shows the number of users who have registered it but I can't see anyway to list these uses. It would be nice to get some feedback from them.

Thanks

Bryan on 8/10/2009 8:34 AM (182 days ago)

Are those keys "sensitive data"? Can I just store it in app settings and use it for user request for tokens?

Andrei on 8/11/2009 6:03 PM (181 days ago)

Works perfectly...

Ujjwol on 8/23/2009 3:34 AM (169 days ago)

Andrei - yes, those keys ARE sensitive data. Or rather, the token you use in subsequent API requests is sensitive, and should be stored in a local encrypted store.

See:

apiwiki.twitter.com/Security-Best-Practices

James Devlin on 8/23/2009 5:13 AM (169 days ago)

I've been coding a OAUTH class for my Twitter app, and i cant make the API calls , its says

"Something is technically wrong.
Thanks for noticing—we're going to fix it up and have things back to normal soon."

thats the return for my api calls, what am i doing wrong?

Gus on 8/27/2009 3:24 PM (165 days ago)

Hello,
i cannot send a tweet with a special charater in it. I always get Unauthorized message. For example, try to tweet a "*" (asterisk) oder a ~ (tilde) or something like german "ä". Can you tell me, how to fix this??

Thank you very much!

Tobi on 9/4/2009 4:21 AM (157 days ago)

sorry my fault, it works, BUT:
if i try to send more than one german special character for example "äääää", then not all characters are sent to twitter. or i get unauthorized message if i try to sent "dsdfögüsdgöäsdfg".

Tobi on 9/4/2009 4:28 AM (157 days ago)

thanks...

m* on 9/26/2009 4:58 PM (135 days ago)

Thanks alot for this awesome article and code. I will be hacking out a client app myself to see how well I'll perform. Your article is a nice head start for anyone serious about developing a Twitter client.

Thanks again -
Helen Neely

Helen Neely on 9/27/2009 3:26 PM (134 days ago)

porno izle

porno izle on 10/4/2009 4:33 PM (127 days ago)

HI,
Thanks for your code. Your code is awesome. We have just get help from it for our web application.

vkamdar on 10/10/2009 2:33 PM (121 days ago)

What about special chars like áéíóú??? I can't make it to work...

ibito on 10/21/2009 4:50 PM (110 days ago)

Thanks for this great job, but things goes wrong when you start working with unicode characters éàèù ect ....
Obviously lots of people experienced this problem
groups.google.com/.../111e7b86133efcaf
code.google.com/p/twitter-api/issues/detail?id=433
They fixed it for the PHP API, but can you do the same for the .net version ?

Thanks again for this great job Smile

Yann ALET on 11/2/2009 1:47 AM (98 days ago)

Hey, what a great article - i love it!
I´ve just tried it and it works fine, but as said above special chars cause a 401 Error. Is there any fix for that?!

Thanks and regards
Heidi

Heidi on 12/8/2009 5:04 AM (62 days ago)

Great post! Had it all up and running in matter of minutes. Brilliant, and thanks again Smile

Rihards on 12/17/2009 4:24 AM (53 days ago)

I recently came across your blog and have been reading along.I thought I would leave my first comment. [url=http://www.sellcheapuggs.com]ugg boots I don’t know what to say except that I have enjoyed reading.cheap ugg boots Nice blog.

ugg boots on 2/8/2010 4:50 AM (14 hours ago)

Comment on this post:

Thanks for your interest in Coding the Wheel. All fields are optional.