Created
December 2, 2014 19:51
-
-
Save jywsn/ff0195c105c23c3bd2f8 to your computer and use it in GitHub Desktop.
Twitter authorization using Birdy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
from flask import Flask, request, session, redirect, url_for | |
from birdy.twitter import UserClient | |
app = Flask(__name__) | |
TWITTER_CONSUMER_KEY = '...' | |
TWITTER_CONSUMER_SECRET = '...' | |
@app.route('/twitter/auth/') | |
def twitter_auth(): | |
""" | |
Initiate twitter authorization | |
""" | |
client = UserClient(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) | |
callback = 'http://'+request.host+url_for('twitter_auth_verify') | |
token = client.get_authorize_token(callback) | |
session['twitter_auth_token'] = token.oauth_token; | |
session['twitter_auth_token_secret'] = token.oauth_token_secret; | |
return redirect(token.auth_url) | |
@app.route('/twitter/auth/verify/') | |
def twitter_auth_verify(): | |
""" | |
Twitter authorization callback | |
""" | |
try: | |
auth_token = session.get('twitter_auth_token') | |
auth_token_secret = session.get('twitter_auth_token_secret') | |
oauth_verifier = request.args.get('oauth_verifier') | |
# Generate authorization token | |
client = UserClient(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, auth_token, auth_token_secret) | |
token = client.get_access_token(oauth_verifier) | |
# Save these values from token somewhere... | |
# token.oauth_token | |
# token.oauth_token_secret | |
# token.user_id | |
# token.screen_name | |
# Clear session vars | |
session.pop('twitter_auth_token') | |
session.pop('twitter_auth_token_secret') | |
# Redirect to page for authorized users | |
return redirect(...) | |
except Exception, e: | |
traceback.print_exc() | |
# Redirect to error page... | |
return redirect(..., error=str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment