-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
119 lines (88 loc) · 3.15 KB
/
Copy pathapp.py
File metadata and controls
119 lines (88 loc) · 3.15 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
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 logging
import os
from flask import Flask, render_template, request, session
from requests_oauthlib import OAuth2Session
from werkzeug.serving import WSGIRequestHandler
REQUIRED_ENVIRONMENT_VARIABLES = (
"FITBIT_CLIENT_ID",
"FITBIT_CLIENT_SECRET",
"FITBIT_REDIRECT_URI",
"FLASK_SECRET_KEY",
)
AUTHORIZATION_BASE_URL = "https://www.fitbit.com/oauth2/authorize"
TOKEN_URL = "https://api.fitbit.com/oauth2/token"
def load_required_configuration():
missing = [
name
for name in REQUIRED_ENVIRONMENT_VARIABLES
if not os.environ.get(name, "").strip()
]
if missing:
raise RuntimeError(
"Missing required environment variables: " + ", ".join(missing)
)
return {name: os.environ[name] for name in REQUIRED_ENVIRONMENT_VARIABLES}
def development_insecure_transport_enabled():
value = os.environ.get("FITBIT_ALLOW_INSECURE_TRANSPORT", "")
return value.strip().lower() in {"1", "true", "yes", "on"}
configuration = load_required_configuration()
FITBIT_CLIENT_ID = configuration["FITBIT_CLIENT_ID"]
FITBIT_CLIENT_SECRET = configuration["FITBIT_CLIENT_SECRET"]
FITBIT_REDIRECT_URI = configuration["FITBIT_REDIRECT_URI"]
if development_insecure_transport_enabled():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
else:
os.environ.pop("OAUTHLIB_INSECURE_TRANSPORT", None)
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
app.secret_key = configuration["FLASK_SECRET_KEY"]
class CredentialSafeRequestHandler(WSGIRequestHandler):
"""Avoid logging request paths or query strings that may contain OAuth data."""
def log_request(self, code="-", size="-"):
self.log(
"info",
'"%s [path redacted] %s" %s %s',
self.command,
self.request_version,
code,
size,
)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/redirect")
def redirect_page():
fitbit = OAuth2Session(FITBIT_CLIENT_ID, redirect_uri=FITBIT_REDIRECT_URI)
token = fitbit.fetch_token(
TOKEN_URL,
client_secret=FITBIT_CLIENT_SECRET,
authorization_response=request.url,
)
session["oauth_token"] = token
return "You are successfully logged in with Fitbit!"
def authenticated_fitbit_session():
return OAuth2Session(FITBIT_CLIENT_ID, token=session.get("oauth_token"))
@app.route("/fetch-steps")
def fetch_steps():
response = authenticated_fitbit_session().get(
"https://api.fitbit.com/1/user/-/activities/steps/date/today/1w.json"
)
return response.json()
@app.route("/fetch-heart-rate")
def fetch_heart_rate():
response = authenticated_fitbit_session().get(
"https://api.fitbit.com/1/user/-/activities/heart/date/today/1w.json"
)
return response.json()
@app.route("/fetch-sleep")
def fetch_sleep():
response = authenticated_fitbit_session().get(
"https://api.fitbit.com/1.2/user/-/sleep/date/today/1w.json"
)
return response.json()
if __name__ == "__main__":
app.run(
debug=False,
port=int(os.environ.get("PORT", "8080")),
request_handler=CredentialSafeRequestHandler,
)