-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_strava.py
More file actions
61 lines (53 loc) · 2.12 KB
/
auth_strava.py
File metadata and controls
61 lines (53 loc) · 2.12 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
import streamlit as st
import requests
import time
import streamlit as st
CLIENT_ID = st.secrets["CLIENT_ID"]
CLIENT_SECRET = st.secrets["CLIENT_SECRET"]
REDIRECT_URI = st.secrets["REDIRECT_URI"]
def get_auth_url():
return (
f"https://www.strava.com/oauth/authorize?"
f"client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}"
f"&approval_prompt=auto&scope=read,activity:read_all"
)
def exchange_token(code):
response = requests.post("https://www.strava.com/oauth/token", data={
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': code,
'grant_type': 'authorization_code'
})
return response.json()
def refresh_token(refresh_token):
response = requests.post("https://www.strava.com/oauth/token", data={
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'refresh_token',
'refresh_token': refresh_token
})
return response.json()
def authenticate():
query_params = st.experimental_get_query_params()
if "access_token" not in st.session_state:
if "code" in query_params:
tokens = exchange_token(query_params["code"][0])
if "access_token" in tokens:
st.session_state["access_token"] = tokens["access_token"]
st.session_state["refresh_token"] = tokens["refresh_token"]
st.session_state["expires_at"] = tokens["expires_at"]
st.experimental_rerun()
else:
st.error("Error during authentication.")
else:
st.markdown("### Connect your Strava account")
st.link_button("🔗 Connect with Strava", get_auth_url())
st.stop()
else:
# Check expiration
if time.time() > st.session_state["expires_at"]:
tokens = refresh_token(st.session_state["refresh_token"])
st.session_state["access_token"] = tokens["access_token"]
st.session_state["refresh_token"] = tokens["refresh_token"]
st.session_state["expires_at"] = tokens["expires_at"]
return st.session_state["access_token"]