errorsandexceptions

Code, etc.

Using OAuth with Python Twitter Tools

I have been writing a Twitter client in Python based on Mike Verdone’s Python Twitter Tools. It’s an elegant and concise library that allows access to all the useful parts of the Twitter API from inside a Python application. Unfortunately, the documentation is bad to non-existent.

After much fiddling I figured out how to use it for OAuth authentication. I’m sharing it here to save the next person the trouble. The first step if of course to import the twitter module. I like to import the oauth_dance portion of it too. This is needed to get the token key and token secret that give the application access to your Twitter account.

import twitter

import twitter.oauth_dance

You will need the consumer key and consumer secret, which you can get by registering your application with Twitter. Once you have done that just stick them in variables. As we don’t yet have the token key or secret, we pass empty strings for them. We then use parse_oauth_tokens in oauth_dance to get the token key and token secret and assign them to variables.

app_name = "appnamehere"

key = "blahblahblahblah"

secret = "putrealdatahere"

twitterConnect = twitter.Twitter(auth=twitter.oauth.OAuth('', '', key, secret), format='')

oauth_token, oauth_token_secret = twitter.oauth_dance.parse_oauth_tokens(twitterConnect.oauth.request_token())

Now we open a web browser and point it at an URL based on the value in oauth_token. The user is asked to enter their login details and confirm that they want to allow the application to access their Twitter account. Then the user is given a PIN that we use in the next stage to complete the OAuth process.

import webbrowser

oauth_url = ('http://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token)

webbrowser.open(self.oauth_url)

Because my application is based on Python and Qt, I get the PIN from a text field in a dialog box. It is an instance of Qt’s QLineEdit. It doesn’t matter how you do it as long as you get the PIN into a string that can be passed to the twitterConnect object.

oauth_verifier = lineEdit.text()

twitterConnect = twitter.Twitter(auth=twitter.oauth.OAuth(oauth_token, oauth_token_secret, key, secret), format='')

oauth_token, oauth_token_secret = twitter.oauth_dance.parse_oauth_tokens(twitterConnect.oauth.access_token(oauth_verifier=oauth_verifier))

That completes the process. Now you can write the OAuth token key and token secret to a configuration file and use them for authentication in the future.

6 responses to “Using OAuth with Python Twitter Tools

  1. Ryan Madsen February 9, 2011 at 10:02 am

    After an hour of googling, I finally stumbled across your site. Thanks for making oauth painless!

  2. Brian Visel April 9, 2011 at 7:05 am

    Seriously, thank you thank you. 🙂 I was just thinking “If I ever figure this out, I’ll have to post it somewhere..”

  3. benc July 23, 2011 at 4:49 pm

    Thanks for this post! You’re right. Scarce documentation for such a great tool.

  4. benc July 23, 2011 at 4:56 pm

    Btw, if you type “twitter -h” on the command line, it sets up the authentication tokens automatically. After doing this, typing “twitter” on the command line got me my first set of tweets.

  5. Trey Jones (@goforIT_griffin) August 11, 2011 at 2:08 am

    I just thought I would chime in here and say that after much trouble I finally got this to work properly by adding the argument ‘api_version = None’ to the twitterConnect assignment line. Prior to adding that line, it was sending me to ‘https://www.api.twitter.com/1/oauth/request_token’
    and returning a 404. Getting rid of the ‘1’ by removing the version information solved this problem and called the correct url: ‘https://www.api.twitter.com/oauth/request_token’

    Thanks for the walkthrough – it was very helpful, though honestly probably not as helpful as the trouble I had which made me read a lot of the code for python twitter tools. No offense.

    • firsthundred100 August 11, 2011 at 8:24 am

      You are using a newer version of Python Twitter Tools. In my version, the default value for the api_version parameter is None. In the most recent version, it is _DEFAULT. Then there is this piece of code, which is not present in the version I was using when I wrote this post.

      if api_version is _DEFAULT:
                  if domain == 'api.twitter.com':
                      api_version = '1'
                  else:
                      api_version = None
      

      This is the root of your problem. Glad you got it sorted out anyway. I will get around to updating the walkthrough.

Leave a reply to firsthundred100 Cancel reply