-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_auth.py
46 lines (37 loc) · 1.41 KB
/
twitter_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# module for setting up authentication with Twitter
from flask import g, session, request, url_for, flash
from flask import redirect, Blueprint
import keys
from flask_oauthlib.client import OAuth
twitterBlueprint = Blueprint('twitter_auth', __name__)
oauth = OAuth(twitterBlueprint)
twitter = oauth.remote_app(
'twitter',
consumer_key=keys.CONSUMER_KEY,
consumer_secret=keys.CONSUMER_SECRET,
base_url="https://api.twitter.com/1.1/",
request_token_url='https://api.twitter.com/oauth/request_token',
access_token_url='https://api.twitter.com/oauth/access_token',
authorize_url='https://api.twitter.com/oauth/authenticate',
)
@twitter.tokengetter
def get_twitter_token():
if 'twitter_oauth' in session:
resp = session['twitter_oauth']
return resp['oauth_token'], resp['oauth_token_secret']
@twitterBlueprint.route('/login')
def login():
callback_url = url_for('twitter_auth.oauthorized', next=request.args.get('next'))
return twitter.authorize(callback=callback_url or request.referrer or None)
@twitterBlueprint.route('/logout')
def logout():
session.pop('twitter_oauth', None)
return redirect(url_for('index'))
@twitterBlueprint.route('/oauthorized')
def oauthorized():
resp = twitter.authorized_response()
if resp is None:
flash('You denied the request to sign in.')
else:
session['twitter_oauth'] = resp
return redirect(url_for('index'))