If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



Twitter authentication using OAuth

by Farooq Kaiser 5. September 2010 04:02

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

reg

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.

callback

 

 

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.

image

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 oauth_token.

Using twitterizer

   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);
   5: Response.Redirect(string.Format("https://twitter.com/oauth/authorize?oauth_token={0}",reqToken.Token)); 

 

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);
   5:  
   6: OAuthTokens accessToken = new OAuthTokens();
   7: accessToken.AccessToken = reqToken.Token;
   8: accessToken.AccessTokenSecret = reqToken.TokenSecret;
   9: accessToken.ConsumerKey = ConsumerKey;
  10: accessToken.ConsumerSecret = ConsumerSecret;
  11:  
  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.

Using TweetSharp

   1: var twitter = FluentTwitter.CreateRequest()
   2: .AuthenticateWith(ConsumerKey, ConsumerSecret,reqToken.Token, reqToken.TokenSecret)
   3: .Statuses().OnListTimeline(screenname, list).AsXml();
   4:  
   5: var response = twitter.Request();
   6:  
   7: var statuses = response.AsStatuses();

Summary

This article showed how to use Twitterizer or TweetSharp to post or get tweets from your application using OAuth.

Currently rated 3.8 by 10 people

  • Currently 3.8/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

.NET | C# | API


comments powered by Disqus
Jobs Autos Real estate Videos Power by Google