Saturday, November 27, 2010

Programmatic Twitter Updates via OAuth and Twitterizer By Peter Bromberg


There is a lot of somewhat confusing information about Twitter's change to requiring OAuth authentication to use the service programmatically. This short FAQ should help.


If you are the owner of a Twitter "App" that you've registered, and all you want to do, for example, is post an update programmatically to your own account, you can get all the tokens you need by simply logging in at dev.twitter.com under that account.

Click on the "Your Apps" link at the top, and then click on the application you want to use programatically.

What you need are the ConsumerKey and the ConsumerSecret, and then if you click on "My Access Token" at the right, you will need the Access Token, and the Access Token Secret.

With these four strings stored in your
The OAuth token, PIN and callback redirect scenarios are only needed if you want to be able to have a user authorize your application on their behalf.

Here is some easy sample code that posts a Twitter update on your account using the Twitterizer library:

OAuthTokens tokens = new OAuthTokens();
tokens.ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
tokens.ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];
tokens.AccessToken = ConfigurationManager.AppSettings["AccessToken"];
tokens.AccessTokenSecret = ConfigurationManager.AppSettings["AccessTokenSecret"];

StatusUpdateOptions options = new StatusUpdateOptions();
TwitterStatus newStatus = TwitterStatus.Update(tokens, message, options).ResponseObject;

-That's all it takes! Of course, your code will need to have a reference to and a using statement for the Twitterizer (Twitterizer2.dll) assembly.  As with any Tweet, you'll need to include your own short url, along with any RT or other key Tweet Tokens you need, and it all must fit into 140 characters in the message variable.

You can find the Twitterizer Google Code repository here.  

No comments:

Post a Comment