-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstravaauth.py
78 lines (54 loc) · 2.43 KB
/
stravaauth.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
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
#########
# stravaauth.py
import os
import time
import json
import requests
from selenium import webdriver
account_file = 'account.json'
token_file = 'strava_tokens.json'
base_path = os.path.dirname(os.path.realpath(__file__))
def get_account(path='{}/conf'.format(base_path)):
af = path + '/' + account_file
with open(af) as account:
account = json.load(account)
return account
def get_tokens(path='{}/conf'.format(base_path)):
tf = path + '/' + token_file
#load token files
with open(tf) as tokenFile:
strava_tokens = json.load(tokenFile)
# get client_id and client_secret
account = get_account(path)
# If current token has expired make post request for new one
if strava_tokens['expires_at'] < time.time():
res = requests.post(
url = 'https://www.strava.com/oauth/token',
data = {
'client_id': account['client_id'],
'client_secret': account['client_secret'],
'grant_type': 'refresh_token',
'refresh_token': strava_tokens['refresh_token'],
'access_token': strava_tokens['access_token']
}
)
newtokens = res.json()
#update token file
with open(tf, 'w') as newStravaTokens:
json.dump(newtokens, newStravaTokens)
strava_tokens = newtokens
return strava_tokens
def site_login(driver):
account = get_account()
driver.get("https://strava.com/login")
driver.find_element_by_id('email').send_keys(account['username'])
driver.find_element_by_id('password').send_keys(account['password'])
driver.find_element_by_id('login-button').click()
#######################
# Step 1: url for when you need new code
# https://www.strava.com/oauth/authorize?scope=read,activity:read_all,activity:write,profile:read_all,read_all&client_id=58450&response_type=code&redirect_uri=http://localhost:8888/strava-call-back.php&approval_prompt=force
# Step 2: get access token
# curl -X POST https://www.strava.com/oauth/token -F client_id=[client_id] -F client_secret=[client_secret] -F code=[CODE from URL] -F grant_type=authorization_code
# client_id is specific to Strava username
# newTokenUrl = "https://www.strava.com/oauth/authorize?scope=read,activity:read_all,profile:read_all,read_all&client_id=&response_type=code&redirect_uri=http://localhost:8888/strava-call-back.php&approval_prompt=force"