Getting started with Twitter\OAuth2\Python

23,349

Almost all oauth examples on blogs seem to be examples of the authorisation phase of oauth and none focus on how to actually make requests once you have these, as once you understand how it works this part is quite obvious. Getting that initial understanding is quite difficult unfortunately.

If you're just trying access your twitter account from a script or app for yourself you can get the access token (called key in the python oauth library) and secret from dev.twitter.com at the bottom of the settings page for your app under the heading Your access token.

import oauth2 as oauth
import json

CONSUMER_KEY = "your app's consumer key"
CONSUMER_SECRET = "your app's consumer secret"
ACCESS_KEY = "your access token"
ACCESS_SECRET = "your access token secret"

consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)

timeline_endpoint = "https://api.twitter.com/1.1/statuses/home_timeline.json"
response, data = client.request(timeline_endpoint)

tweets = json.loads(data)
for tweet in tweets:
    print tweet['text']

This example is using the python lib python-oauth2, which is an unfortunately named OAuth library not an OAuth2 library.

If you want to actually let other people authorise their account to be used by your app then you need to implement the redirect dance where you ask twitter for a request token/secret pair and then redirect the user to the twitter authorize page with this request token, they sign in and authorize the token and get redirected back to your application, you then exchange the request token for an access token and secret pair which you can store and use to make requests like above.

The Twitter Three-legged OAuth Example in the Readme at http://github.com/simplegeo/python-oauth2 seems to cover what needs to be done

Share:
23,349
Sean
Author by

Sean

Updated on August 10, 2020

Comments

  • Sean
    Sean almost 4 years

    I'm attempting to connect to twitter using python, and I'm finding it really frustrating.
    Everything I read suggests that I need a consumer key, a consumer secret, an access key and an access secret - for example: Using python OAUTH2 to access OAUTH protected resources

    I can get the consumer key and the consumer secret from the twitter settings page for the little test app I created, but what about the other two? After a bit of googling it seems everyone thinks it's so obvious where you get this info from that it's not worth putting up, so I might be having a really dumb moment but could someone please spell it out for idiots like me please?

    Edit:
    OK to get these details open your app settings in Twitter and click the "My Access Token" link.
    I suppose when looking for an Access Token, if you were to click on a link titled "My Access Token" might help. I'd love to attribute my stupidity to the wine, but really I don't know...

  • atp
    atp over 12 years
    You deserve a medal for this answer. I've been scouring the web for details on how to simply get some information from my own account, and this is the only comprehensive answer I can find.
  • Ed Randall
    Ed Randall almost 12 years
    There seem to be dozens of libraries and wrappers calling themselves oauth or oauth2, which exact one is this?
  • Ed Randall
    Ed Randall almost 12 years
    Using google app engine I get: import oauth2 as oauth ImportError: No module named oauth2
  • Evgeny
    Evgeny over 11 years
    simplegeo oauth2 DOES NOT support OAuth2 protocol at this time it is version one protocol.
  • Zemogle
    Zemogle about 11 years
    Excellent and concise answer. It's in need of updating because the twitter v1 API has been deprecated in favour of 1.1, i.e. timeline_endpoint = "api.twitter.com/1.1/statuses/home_timeline.json"
  • x0b
    x0b over 10 years
    Thanks for this answer, really helped me out.
  • Adam Obeng
    Adam Obeng about 10 years
    The Twitter API now requires access over HTTPS, so the URL should be https://api.twitter.com/1.1/statuses/home_timeline.json
  • MagicLAMP
    MagicLAMP almost 10 years
    This did not work for me. Reference to oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET) gives the following error: AttributeError: 'module' object has no attribute 'Consumer'
  • MagicLAMP
    MagicLAMP over 9 years
    Correction. I got it to work, once I got the right python oauth package at github.com/simplegeo/python-oauth2
  • Piyush S. Wanare
    Piyush S. Wanare almost 6 years
    can anyone please guide me for engagement API? I want to fetch impressions , as I have enterprise account.