Coding the Tweet: Building a Custom Branded Twitter Application
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.
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:
- The ability to allow you to post tweets from your custom Twitter application, tagged with the details you specified above.
- Integration with oAuth, the recommended Twitter authentication mechanism.
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.
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:
- Accept the Consumer Key and Consumer Secret from the user.
- Ask Twitter for the URL of the “authorize this application” page on Twitter.
- Open the default web browser and navigate to that page.
- Allow the user to specify the PIN he was provided by Twitter.
- Bundle this PIN in the call to the http://twitter.com/oauth/access_token API.
- 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.)
- Ryios on How To Inject a Managed .NET Assembly (DLL) Into Another Process
- Christopher Galpin on EJ68
- Daniel on Git for the Antisocial: Setting Up a Local-Only Git Repository
- uncloaked on Git for the Antisocial: Setting Up a Local-Only Git Repository
- PenguinQueenHaley HP&DMfan; forever on Summoning the Harry Potter MMORPG