-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
68 lines (50 loc) · 1.4 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
import os
from flask import Flask, redirect, render_template_string, request
from werkzeug import Response
from raindropio import *
client_id = os.environ["RAINDROP_CLIENT_ID"]
client_secret = os.environ["RAINDROP_CLIENT_SECRET"]
redirect_uri = "http://localhost:5000/approved"
app = Flask(__name__)
INDEX = """
<html>
<a href="./login">Click here for login.</a>
</html>
"""
COLLECTIONS = """
<html>
<ul>
{% for collection in collections %}
<li>{{collection.title}}
{% endfor %}
</ul>
</html>
"""
@app.route("/approved")
def approved() -> str:
oauth = create_oauth2session(client_id, redirect_uri=redirect_uri)
code = request.args.get("code")
token = oauth.fetch_token(
API.URL_ACCESS_TOKEN,
code=code,
client_id=client_id,
client_secret=client_secret,
include_client_id=True,
)
with API(
token,
client_id=client_id,
client_secret=client_secret,
) as api:
collections = Collection.get_roots(api)
return render_template_string(COLLECTIONS, collections=collections)
@app.route("/login")
def login() -> Response:
oauth = create_oauth2session(client_id, redirect_uri=redirect_uri)
authorization_url, _ = oauth.authorization_url(API.URL_AUTHORIZE)
return redirect(authorization_url)
@app.route("/")
def index() -> str:
return render_template_string(INDEX)
if __name__ == "__main__":
app.run(debug=True)