-
-
Save echarles/87706b16dd22263e11329c88169393cd to your computer and use it in GitHub Desktop.
Flask + python-oauth2 + python-twitter sample
This file contains hidden or 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
# app/config.py | |
# -*- coding: utf-8 -*- | |
import os | |
class Config(object): | |
DEBUG = False | |
SQLALCHEMY_ECHO = False | |
SECRET_KEY = 'dev_key_h8hfne89vm' | |
CSRF_ENABLED = True | |
CSRF_SESSION_LKEY = 'dev_key_h8asSNJ9s9=+' | |
class DevelopmentConfig(Config): | |
DEVELOPMENT = True | |
DEBUG = True | |
SQLALCHEMY_ECHO = True | |
TWITTER_OAUTH_CONSUMER_KEY = 'your consumer key' | |
TWITTER_OAUTH_CONSUMER_SECRET = 'your consumer secret' | |
class TestingConfig(DevelopmentConfig): | |
TESTING = True | |
class ProductionConfig(Config): | |
PRODUCTION = True | |
mode = os.environ.get('TFAVFEED_ENV', 'development') | |
object = DevelopmentConfig | |
if mode == 'development': | |
object = DevelopmentConfig | |
elif mode == 'testing': | |
object = TestingConfig | |
elif mode == 'production': | |
object = ProductionConfig | |
else: | |
raise ValueError("Unknown config mode.") |
This file contains hidden or 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
# app/main.py | |
# -*- coding: utf-8 -*- | |
from flask import Flask | |
import config | |
app = Flask(__name__) | |
app.config.from_object(config.object) | |
app.logger.info("config.object = %s" % config.object) | |
from views import root | |
app.register_blueprint(root.app) | |
if __name__ == '__main__': | |
app.run(debug = app.config['DEBUG']) |
This file contains hidden or 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
# app/views/root.py | |
# -*- coding: utf-8 -*- | |
from flask import ( | |
Blueprint, | |
redirect, | |
render_template, | |
session, | |
url_for, | |
) | |
import oauth2 as oauth | |
import twitter | |
import urllib | |
import config | |
# parse_qsl moved to urlparse module in v2.6 | |
try: | |
from urlparse import parse_qsl | |
except: | |
from cgi import parse_qsl | |
app = Blueprint('root', __name__) | |
oauth_consumer = oauth.Consumer( | |
key = config.object.TWITTER_OAUTH_CONSUMER_KEY, | |
secret = config.object.TWITTER_OAUTH_CONSUMER_SECRET, | |
) | |
oauth_client = oauth.Client(oauth_consumer) | |
@app.route('/login', methods = [ 'GET', 'POST' ]) | |
def login(): | |
res, content = oauth_client.request( | |
'http://api.twitter.com/oauth/request_token?%s' | |
% urllib.urlencode({ | |
'oauth_callback': 'http://localhost:5000' + url_for('root.oauth_authorized') | |
}).replace('+', '%20'), | |
'GET', | |
) | |
if res['status'] != '200': | |
raise Exception( | |
"Invalid response %s: %s" % (res['status'], content) | |
) | |
request_token = dict(parse_qsl(content)) | |
session['request_token'] = request_token | |
url = 'https://api.twitter.com/oauth/authorize?%s' % ( | |
urllib.urlencode({ | |
'oauth_token': request_token['oauth_token'], | |
'oauth_callback': 'http://localhost:5000' + url_for('root.oauth_authorized') | |
}).replace('+', '%20'), | |
) | |
return redirect(url) | |
@app.route('/oauth_authorized') | |
def oauth_authorized(): | |
request_token = session['request_token'] | |
token = oauth.Token( | |
request_token['oauth_token'], | |
request_token['oauth_token_secret'] | |
) | |
client = oauth.Client(oauth_consumer, token) | |
res, content = client.request( | |
'https://api.twitter.com/oauth/access_token', | |
'POST' | |
) | |
if res['status'] != '200': | |
raise Exception( | |
"Invalid response %s: %s" % (res['status'], content) | |
) | |
access_token = dict(parse_qsl(content)) | |
api = twitter.Api( | |
consumer_key = config.object.TWITTER_OAUTH_CONSUMER_KEY, | |
consumer_secret = config.object.TWITTER_OAUTH_CONSUMER_SECRET, | |
access_token_key = access_token['oauth_token'], | |
access_token_secret = access_token['oauth_token_secret'], | |
) | |
return str(api.VerifyCredentials()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment