-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
42 lines (36 loc) · 1.19 KB
/
auth.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
from stravalib.client import Client
from dotenv import load_dotenv, dotenv_values
import json
import os
load_dotenv()
CLIENT_ID = os.environ.get("CLIENT_ID")
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
client = Client()
def get_authorization_url(redirect_uri):
return client.authorization_url(client_id=CLIENT_ID, redirect_uri=redirect_uri)
def exchange_code_for_token(code):
token_response = client.exchange_code_for_token(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
code=code,
)
tokens = {
"access_token": token_response["access_token"],
"refresh_token": token_response["refresh_token"],
"expires_at": token_response["expires_at"],
}
with open("tokens.json", "w") as token_file:
json.dump(tokens, token_file)
return tokens
def refresh_access_token():
with open("tokens.json", "r") as file:
tokens = json.load(file)
new_token = client.refresh_access_token(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
refresh_token=tokens["refresh_token"],
)
tokens.update(new_token)
with open("tokens.json", "w") as file:
json.dump(tokens, file)
return tokens