How to use oauth-python-twitter

April 28, 2009

Using OAuth for authentication is quite different then using Basic Auth in many ways, first major difference is that one don’t know the username until it is requested from twitter after completing the authentication process.


You will need following information to start.

  • Consumer Key
  • Consumer Secret

If you do not have above, you can request them from here: http://twitter.com/oauth_clients

The following are the steps that should be performed to authenticate user and get user information from twitter, the rest is same as using python-twitter.

  • Get the Request Token from twitter
  • Get Authorization URL
  • Get the Access Token from twitter
  • Get user information

Get the Request Token from twitter


request_token = twitter.getRequestToken()

Get Authorization URL

using same request_token from previous step

authorization_url = twitter.getAuthorizationURL(request_token)

Now send the user to authorization URL, for allowing access to the application.

Get the Access Token from twitter

Once the user return from twitter, we need to request the access_token from twitter for futher aunthenticated api calls on behalf of user.

NOTE: Need to create the new instance of OAuthApi using request_token from first step.

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, request_token)

access_token = twitter.getAccessToken()

Get user information

This api call was not present in python-twitter and the user info call expected that there we know the username before making call, but this is not the case with OAuth, so we have to request User Information so we can use it in our application.

NOTE: Need to create the new instance of OAuthApi using access_token from last step.

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)

user = twitter.GetUserInfo()

Now use the rest of python-twitter api calls as you used to use them with basic authentication, for more information see the wiki at:http://code.google.com/p/python-twitter

If you like this post, share it with others:
add to del.icio.us :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: reddit :: fark :: TailRank :: post to facebook


  1. Hello there! Awesome job with your oauth/twitter python libraries integration!

    I just want to ask you one little thing to avoid any issues beforehand: which version of python-twitter is required by your project, 0.5 or the trunk version?

    Thanks for your work!

    • Hello,

      You need to have a trunk version. I am glad it was useful for you.

  2. Hi there, I seem to be stuck and can’t figure out why.

    What OAuth library are you using? I can’t seem to find where this is:


    • Oops, silly me. I found the module. I’m now having a problem getting the access token.

      urllib2.HTTPError: HTTP Error 401: Unauthorized

      I’m doing this completely from a python shell. After I plug in the authorization URL in a browser and permit the OAuth consumer, I get a PIN from Twitter.

      Am I supposed to use this PIN during the request for an access token ?

  3. What happen if user presses refresh button. My question is how to use this request token if user press refresh button?

    I am stuck on it.

    Thanks for any reply.

  4. This doesn’t work without providing the PIN returned from twitter. Where am I supposed to do that?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: