-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
56 lines (47 loc) · 1.78 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
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, render_template, session, url_for
import json
import os
import pprint
app = Flask(__name__)
app.secret_key = os.urandom(24)
client_id = os.environ["OAUTH_APP_ID"]
client_secret = os.environ["OAUTH_SECRET"]
domain = os.environ.get("DOMAIN", "http://localhost:5000")
api_url = "https://api.planningcenteronline.com"
token_url = f"{api_url}/oauth/token"
def token_updater(token):
session["oauth_token"] = token
pco = OAuth2Session(client_id,
scope="people services",
redirect_uri=f"{domain}/auth/complete",
auto_refresh_kwargs={
"client_id": client_id,
"client_secret": client_secret,
},
auto_refresh_url=token_url,
token_updater=token_updater)
@app.route("/")
def index():
if "oauth_token" in session:
response = pco.get(f"{api_url}/people/v2/people").json()
formatted_response = pprint.PrettyPrinter(indent=2).pformat(response)
return render_template("index.html", logged_in=True, people=response['data'], formatted_response=formatted_response)
else:
return render_template("login.html")
@app.route("/auth")
def auth():
authorization_url, state = pco.authorization_url(f"{api_url}/oauth/authorize")
session['oauth_state'] = state
return redirect(authorization_url)
@app.route("/auth/complete", methods=["GET"])
def callback():
token = pco.fetch_token(token_url, client_secret=client_secret,
authorization_response=request.url)
session["oauth_token"] = token
return redirect("/")
@app.route("/auth/logout")
def logout():
pco.post(f"{api_url}/oauth/revoke", data={"token": session["oauth_token"]})
del session["oauth_token"]
return redirect("/")