Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (60 loc) · 2.33 KB
/
Copy pathapp.py
File metadata and controls
80 lines (60 loc) · 2.33 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
from pprint import pprint as pp
import ssl
import sys
from flask import Flask, render_template, request, g
from flask import session, flash, redirect, url_for
from flask_oauthlib.client import OAuth
app = Flask(__name__)
app.secret_key = os.environ.get('PYB_100D_APP_SECRET') or sys.exit('need secret app key')
oauth = OAuth(app)
# urllib.error.URLError: <urlopen error
# [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>
ssl._create_default_https_context = ssl._create_unverified_context
twitter = oauth.remote_app(
'twitter',
consumer_key=os.environ.get('PYB_100D_TW_KEY') or sys.exit('need tw key'),
consumer_secret=os.environ.get('PYB_100D_TW_SECRET') or sys.exit('need tw 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/authorize'
)
@twitter.tokengetter
def get_twitter_token():
if 'twitter_oauth' in session:
resp = session['twitter_oauth']
return resp['oauth_token'], resp['oauth_token_secret']
@app.before_request
def before_request():
g.user = None
if 'twitter_oauth' in session:
g.user = session['twitter_oauth']
@app.route('/login')
def login():
# this snippet works better:
# https://github.com/mitsuhiko/flask-oauth/blob/master/example/tweet.py
return twitter.authorize(callback=url_for('oauthorized',
next=request.args.get('next') or request.referrer or None))
@app.route('/logout')
def logout():
session.pop('twitter_oauth', None)
return redirect(request.referrer or url_for('index')) # fix: redirect to previous url
@app.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'))
return redirect(request.args.get('next')
or request.referrer or
url_for('index')) # fix: redirect to previous url
@app.route('/', methods=['GET', 'POST'])
def index():
if g.user:
pp(g.user['screen_name'])
return render_template("login.html")
if __name__ == '__main__':
app.run(debug=True)