As of August 31,2010, Twitter has stopped using basic authentication. applications will all use OAuth, an authentication method that lets you use apps without them storing your password. To learn more about OAuth, please visit @ http://oauth.net.
Register your new application with Twitter
You'll need a name and url for your application in order to register it, and you'll need to define a callback url. The callback url is the full url of the page Twitter should send the user to after it's done authenticating.
Once you've registered your application, Twitter will issue you a Consumer Key and a Consumer Secret for your new app. It's used in your code so that Twitter can identify your application when you're making API calls.
In this article, i will use twitterizer library which is available @ http://www.twitterizer.net/
When the user clicks the "Sign in with Twitter" link and they are redirected to the Twitter website. Here they are asked whether they want to allow or deny the application's request to connect to their account.
If the user allows the application access to their account then Twitter redirects the user to the application's Callback URL passing along a token in a querystring parameter named
1: // Twitter SignIn
2: string ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"]; // Your ConsumerKey
3: string ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]; // Your ConsumerSecret
4: OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(ConsumerKey, ConsumerSecret);
Your callback method will look as shown below.
1: string requestToken = Request.QueryString["oauth_token"].ToString();
2: string ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
3: string ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"];
4: OAuthTokenResponse reqToken = OAuthUtility.GetAccessToken(ConsumerKey, ConsumerSecret, requestToken);
6: OAuthTokens accessToken = new OAuthTokens();
7: accessToken.AccessToken = reqToken.Token;
8: accessToken.AccessTokenSecret = reqToken.TokenSecret;
9: accessToken.ConsumerKey = ConsumerKey;
10: accessToken.ConsumerSecret = ConsumerSecret;
12: Twitterizer.TwitterStatus.Update(accessToken , "Your tweet goes here");
In my last article, i have shown basic authentication using TweetSharp. Since Twitter does not support basic Authentication anymore. Here is an updated code for TweetSharp.
1: var twitter = FluentTwitter.CreateRequest()
2: .AuthenticateWith(ConsumerKey, ConsumerSecret,reqToken.Token, reqToken.TokenSecret)
3: .Statuses().OnListTimeline(screenname, list).AsXml();
5: var response = twitter.Request();
7: var statuses = response.AsStatuses();
This article showed how to use Twitterizer or TweetSharp to post or get tweets from your application using OAuth.