-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
119 lines (87 loc) · 3.49 KB
/
app.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import logging
import urllib
from urlparse import urlparse, urlunparse
from flask import (Flask,
flash,
render_template,
request,
redirect,
session,
url_for)
import requests
app = Flask(__name__)
app.debug = os.environ.get("APP_ENV") == "dev"
app.secret_key = os.environ.get("APP_SECRET_KEY")
CLIENT_ID = os.environ.get("CLIENT_ID")
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
AUTHORIZE_URL = "https://seatgeek.com/oauth2"
API_BASE = "https://api.seatgeek.com/2"
SCOPES = "email,readwrite"
class SGAccessTokenAuth(requests.auth.AuthBase):
def __init__(self, access_token=None):
self.access_token = access_token
def __call__(self, r):
r.headers["Authorization"] = "token {}".format(self.access_token)
return r
@app.before_request
def redirect_www_ssl():
"""Redirect www requests to non-www and non-ssl to ssl."""
urlparts = urlparse(request.url)
urlparts_list = list(urlparts)
should_redirect = False
if urlparts.netloc == 'www.ericwaller.com':
urlparts_list[1] = 'ericwaller.com'
should_redirect = True
# if os.environ.get("APP_ENV") == 'prod' and urlparts.scheme == 'http':
# urlparts_list[0] = 'https'
# should_redirect = True
if should_redirect:
return redirect(urlunparse(urlparts_list), code=301)
@app.route("/")
def index():
user = None
if "access_token" in session:
with requests.Session() as s:
s.auth = SGAccessTokenAuth(access_token=session["access_token"])
r = s.get(API_BASE + "/me")
app.logger.info(r.json())
if r.status_code == requests.codes.ok:
user = r.json()
else:
del session["access_token"]
return render_template("index.html", user=user)
@app.route("/api/connect")
def oauth_connect():
redirect_uri = url_for("oauth_callback", _external=True)
params = dict(client_id=CLIENT_ID, scope=SCOPES, redirect_uri=redirect_uri)
return redirect(AUTHORIZE_URL + "?" + urllib.urlencode(params))
@app.route("/api/callback")
def oauth_callback():
# Success args: code, state
code = request.args.get("code")
if code:
app.logger.info("OAuth success, code:%s", code)
params = dict(code=code, grant_type="authorization_code")
r = requests.get(API_BASE + "/oauth/access_token", params=params, auth=(CLIENT_ID, CLIENT_SECRET))
if r.status_code == requests.codes.ok:
resp = r.json()
app.logger.info("OAuth retrieved access_token:%s", resp["access_token"])
session["access_token"] = resp["access_token"]
flash("You connected with SeatGeek!")
return redirect(url_for("index"))
# Error args: error_reason, error, error_description, state
error = request.args.get("error")
error_reason = request.args.get("error_reason")
error_description = request.args.get("error_description")
app.logger.info("OAuth error, error:%s error_reason:%s", error, error_reason)
flash("You didn't connect with SeatGeek...")
return redirect(url_for("index"))
@app.route("/keybase.txt")
def keybase():
return render_template("keybase.txt"), 200, {'Content-Type': 'text/plain'}
if __name__ == "__main__":
app.logger.addHandler(logging.StreamHandler())
app.logger.setLevel(logging.INFO)
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port)