-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
35 lines (28 loc) · 1.09 KB
/
Copy pathauth.py
File metadata and controls
35 lines (28 loc) · 1.09 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
"""Shared Strava auth helpers.
Loads credentials from .env, refreshes the access token (Strava expires
them every six hours), persists the rotated tokens back to .env, and
returns an authenticated stravalib Client.
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv, set_key
from stravalib import Client
ENV_PATH = Path(__file__).parent / ".env"
load_dotenv(ENV_PATH)
def get_client() -> Client:
client_id = os.getenv("STRAVA_CLIENT_ID")
client_secret = os.getenv("STRAVA_CLIENT_SECRET")
refresh_token = os.getenv("STRAVA_REFRESH_TOKEN")
if not all([client_id, client_secret, refresh_token]):
sys.exit("Missing credentials in .env. Run do_oauth.py first.")
client = Client()
tokens = client.refresh_access_token(
client_id=int(client_id),
client_secret=client_secret,
refresh_token=refresh_token,
)
set_key(str(ENV_PATH), "STRAVA_ACCESS_TOKEN", tokens["access_token"])
set_key(str(ENV_PATH), "STRAVA_REFRESH_TOKEN", tokens["refresh_token"])
client.access_token = tokens["access_token"]
return client