Shakya Milind

Month

March 2011

4 posts

Overriding Twitter's Default Callback Url using Oauth

Unlike Facebook, Twitter does not let you specifiy your callback url when you are initiating Twitter’s Oauth procedure. The default Callback Url is the one specified when you registered your application here https://dev.twitter.com/apps/new . If you wanted to change your callback url, you would have to manually log in to your app settings in https://dev.twitter.com/apps and edit them. This is annoying for various reasons.Suppose you have a single twitter application, that you want to use on multiple sites. For example, let us say Groupon, has two sites. One for its customers and one for its clients,customer.groupon.com and clients.groupon.com. When a customer clicks on ‘Connect to Twitter’ in the customer website, let’s say it uses the default callback url in the twitter website, customer.groupon.com. But when a client clicks the ‘Connect to Twitter’ to link the clients account with twitter, so it can tweet new deals everytime it makes a deal on groupon, the default callback needs to be overriden so it becomes clients.groupon.com instead of customer.groupon.com.

Turns out the default callback url can be overriden very easily, though not straighforward at first.

 If you wanted to use the default callback url, you just send a HTTP GET request to ’http://twitter.com/oauth/request_token’ and ‘http://twitter.com/oauth/access_token’.

If you wanted to override the default callback URL, you would send a HTTP POST request to the request token url and access token url with  

body= urllib.urlencode(‘oauth_callback’:’site-you-want-to-redirect-to’).

Now, you can override, twitter’s default callback url, depending on which site you are using your ‘Connect to Twitter’ feature.

Mar 30, 2011
Twitter Connect in django using Oauth

I was recently involved in a django project where I had the opportunity to learn Oauth2.0. Almost all new social networking sites have started using Oauth2.0 in favour of basic authentication which was the prevalent authentication system sometime ago. Twitter has not yet migrated to using Oauth2.0, it is using Oauth1.0a as of now.

I am specifically going to write about how I integrated twitter authentication into my django-app. I am assuming the reader is familiar with python and django, or the MVC framework in general.First you are going to need your twitter consumer key and your twitter consumer secret, which you will receive once you have registered your new application with twitter here https://dev.twitter.com/apps/new . Unlike facebook, in twitter you have to explicitly specify your Oauth Callback Url, while registering your app. This limits you to use the same callback url that you have specified in your twitter settings page.So, if your domain name has changed, which can occur multiple times in your development environment for various reasons, you would want to override the twitter oauth callback url . This is a bit tricky at first but it is easy once you know how to do it. I will describe the steps to override twitter oauth callback url in my next post.

I wanted my twitter-connect view in my django app, to store the user’s twitter access_token, access_token_secret , their user_id and their twitter username. So, I set up my model accordingly.

You will need python-oauth2 library to communicate with twitter using the oauth protocol. You can get the latest version of python-oauth2 from https://github.com/simplegeo/python-oauth2.

Once you have the python-oauth2 library installed, you are ready to write the initial piece of code for your twitter connect view.

It is best to store all your twitter consumer key and your twitter consumer secret in the settings.py file in your django application. Here I am checking for a ‘redirect’ in my request.session, if you have already stored your twitter credentials in the database then it will redirect you to the dashboard, else you will follow the regular twitter authentication protocol.

It is important to store the session variable because, authentication via Ouath2.0 is a three stage process. First, using our twitter consumer key and consumer secret, we tell to twitter that we are a valid application, twitter upon verifying this, will send us a request_token and request_token_secret. We store these two tokens in our session variables. Then we set up the new authorize url as shown below and redirect to the new url without our request token.

Then, twitter upon receiving the correct request token, sends us a oauth verifer. We get the oauth verifier using request.GET and we set the token using the request token, request token secret and the newly received oauth verifier.

In the content thus recieved, we will have a json object or the access_token, access_token_secret,user_id, and screenname. We have to store this access_token in our database, if we want to post to our user’s twitter account using the access_token.

Once you have the user’s access token, tweeting on behalf of the user is super simple.

You just submit a Post request to ”https://api.twitter.com/1/statuses/update.json” using the required oauth credentials we have stored in our database. Here, data is a dict, consisting of our tweet. Twitter, calls it ‘status’ so, we update ‘status’ to our new tweet and send a post request to our request_uri. 

If you followed the steps correctly, you should be able to use oauth to successfully tweet to a user’s twitter feed using your django view.

Mar 29, 2011
Pasting in VIM

Sometimes I need to paste something to my code in VIM. Needless to say if you don’t know how to use the paste functionality properly in vim, it pretty much screws the indentation of the copied text. That’s when :set paste comes in. When you want to paste to your file, just enable set and paste, the indentation will remain intact.

If you are like me and do not want to type :set paste everytime you want to paste,you can include “set pastetoggle=<f1>” in your .vimrc file. Basically what it does is, it enables you to toggle between  the paste mode and the normal insert mode by pressing f1. Pretty Handy.

Mar 25, 201111 notes
#VIM #paste #toggle
Git: Merging a single file from one branch to another.

Today I had a case where I had to merge a single file from our master branch to our experimental branch. I did not want to merge these two branches for I just wanted to merge the changes in a single file in my master to the file in my experimental branch. 

Turns out git has a clean method of doing just that.

git checkout master <path to the file you want to merge>

Mar 17, 2011
#git
Next page →
2011 2012
  • January 1
  • February
  • March 1
  • April 1
  • May
  • June
  • July
  • August
  • September
  • October
  • November
  • December
2011 2012
  • January
  • February
  • March 4
  • April 1
  • May 2
  • June 3
  • July
  • August 1
  • September 2
  • October 1
  • November
  • December