twitter applications and oauth in python with tweepy and flask

I want to build a larger scale twitter application, which would require a user to authenticate.  I found some great material on the Tweepy documentation on doing this, but there were some parts of the tutorial I was unfamiliar with, so I thought I’d fill in those holes here.  Most python oauth examples I found online assumed you already had the access tokens.  What I wanted was a way to get that ‘authorize’ button.

Why would you want a user to authenticate?

You can make a certain amount of API calls if you authenticate an app in your own account, but if you really want to scale an app up you would consume too many API calls running methods for everyone who uses your app with your own keys.  This way everyone who uses the app generates keys and can use the api to get information related to their own accounts.

Get authenticated

I went with Flask to write the server side stuff since I am most familiar with python, and it was quick to get started.  I chose Tweepy as a python twitter library since it has oauth support.

You can grab the code from this example on github.  Make sure you have both those libraries installed.  The bulk of what I’ll talk about is in the server.py script, since that’s where the authentication occurs.  The static and template directories contain files that I use to render the results from the api object after authentication.

Here is the script, I’ll follow it with some details.

 

First fill out the consumer token stuff.

CONSUMER_TOKEN=’fill this out’

CONSUMER_SECRET=’and this’

CALLBACK_URL = ‘http://localhost:5000/verify’

I chose the callback at localhost:5000 since that’s the default for Flask.  You can get the consumer token and secret from dev.twitter.com after registering an app.

You’ll see in the code I use a couple dicts to save data, session and db.  You can substitute a database to hold the data instead of dictionaries.

After starting the server with >python server.py , if you head into your browser and go to ‘http://localhost:5000/’ you’ll execute the code in the @app.route(‘/’) block.  This is where authentication begins.  I use the consumer token and secret to generate a request token key and secret, which I save for a later step in the session dict. Then you obtain a redirect url from twitter which brings up a screen asking the user if they want to authenticate your app.  In the URL you’ll see the redirect_url.

You can see here I used the same tokens for a previous app, so thats why you see the title has to do with tweet maps.  After the user clicks ‘Authorize app’ they are redirected to “/verify” as specified in the call back url.  I use the request object to get the oauth_verifier, and set the request tokens based on what I saved them as earlier. I then use the verifier received in the returned url to gain access

auth.get_access_token(verifier)

Finally I save the token, secret, and api object in a db dict to access later.  The access token and secret do not expire.

db['api']=api

db['access_token_key']=auth.access_token.key

db['access_token_secret']=auth.access_token.secret

I reroute the app and retrieve the api object from the database (in start()).  Now you can make API calls that would have required authentication.  In future sessions I can instead authenticate with the access token key and secret.

Make something cool with the data

Now for a quick example of what you can do.  Using this api object  I retrieve the user’s latest tweets with tweets=api.user_timeline(), and send that list of objects tweets to render as ‘tweets.html’ with Flask’s render_template function. In the template I simply have

{% for t in tweets %}

<div id=’tweet’>{{ t.text }}</div>

{% endfor %}

So that after the app is authorized you can see, in this case, my most recent tweets.

You can do many other things, and I point you to the Tweepy API documentation to get some ideas.

Embed This

4 Responses to “twitter applications and oauth in python with tweepy and flask”

  1. toby says:

    You legend, this is exactly what I’ve been looking for.

    Thanks!

  2. kawandeep says:

    Glad to hear that! Looked around anly.tk, I’m diggin the analysis you did on tweets for MWC. Looking forward to seeing your next twitter projects

  3. Neyuh says:

    Thanks, exactly what I needed!

    Tip for when you get a 401 error when trying to set the redirect_url: Just set a callback url in your twitter application page at dev.twitter.com ;)

  4. Ryan says:

    Neyuh thanks for that. I kept getting 401′s when passing the callback parameter. I just needed to set a dummy callback in the app settings. Doesn’t make much sense but it works.

Leave a Reply