AverageSecurityGuy

Security, Programming, Pentesting

About

Mastodon

Linked In

Projects

Cheat Sheets

Book

30 January 2013

Twitter Single User Oauth

by {"login"=>"averagesecurityguy", "email"=>"stephen@averagesecurityguy.info", "display_name"=>"averagesecurityguy", "first_name"=>"", "last_name"=>""}

I was helping a friend with a Python script he was using to query the Twitter search API and I decided I wanted to write a simple Twitter client in Python. Twitter allows users to use two OAuth authorization methods, three-legged and single user. Most OAuth examples and libraries are centered around three-legged authorization, which requires an application to call a Twitter authentication page, so the user can input his or her username and password and then calls back to another URL with the necessary access tokens. This sounded overly complex for what I wanted, so I started looking at single user authorization. I am sure the standard OAuth libraries can handle single user authorization just as well as three-legged authorization but I also wanted to understand the OAuth protocol better, so I wrote my own single user OAuth module using the information here and here. This module integrates with the Python Requests library so accessing Twitter is as easy as:

import requests
import twitter_auth

ssn = requests.Session
ssn.auth = twitter_auth.TwitterSingleOAuth(consumer_key,
                                           consumer_secret,
                                           access_token,
                                           access_token_secret)

resp = ssn.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json')
print resp.json()

To use the library you will need to sign in to dev.twitter.com and create a new application and get your consumer key, consumer secret, access token, and access token secret. Don't share the consumer secret or the access tokens with anyone, including Github. You can find the Twitter single user OAuth library here.

Have fun, and as always, let me know if there are any problems.

tags: OAuth - python - Python Requests Twitter Authentication - Requests - Twitter