Private
Public Access
1
0

strava auth seems to work

This commit is contained in:
Sander Roosendaal
2018-11-13 16:32:45 +01:00
parent f1d53db627
commit 04e06051cf
5 changed files with 76 additions and 11 deletions

View File

@@ -40,11 +40,11 @@ oauth_data = {
'autorization_uri': "https://www.strava.com/oauth/authorize",
'content_type': 'application/json',
'tokenname': 'stravatoken',
'refreshtokenname': '',
'expirydatename': '',
'refreshtokenname': 'stravarefreshtoken',
'expirydatename': 'stravatokenexpirydate',
'bearer_auth': True,
'base_url': "https://www.strava.com/oauth/token",
'grant_type': None,
'grant_type': 'refresh_token',
}
@@ -52,6 +52,27 @@ oauth_data = {
def get_token(code):
return imports_get_token(code, oauth_data)
def strava_open(user):
return imports_open(user, oauth_data)
def do_refresh_token(refreshtoken):
return imports_do_refresh_token(refreshtoken, oauth_data)
def rower_strava_token_refresh(user):
r = Rower.objects.get(user=user)
res = do_refresh_token(r.stravarefreshtoken)
access_token = res[0]
expires_in = res[1]
refresh_token = res[2]
expirydatetime = timezone.now()+timedelta(seconds=expires_in)
r.stravatoken = access_token
r.stravatokenexpirydate = expirydatetime
r.stravarefreshtoken = refresh_token
r.save()
return r.stravatoken
# Make authorization URL including random string
def make_authorization_url(request):
return imports_make_authorization_url(oauth_data)
@@ -62,6 +83,9 @@ def get_strava_workout_list(user,limit_n=0):
if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate):
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
# ready to fetch. Hurray
authorizationstring = str('Bearer ' + r.stravatoken)
@@ -86,10 +110,13 @@ def get_strava_workouts(rower):
if not isprorower(rower):
return 0
res = get_strava_workout_list(rower.user,limit_n=10)
print res.status_code
try:
thetoken = strava_open(rower.user)
except NoTokenError:
return 0
res = get_strava_workout_list(rower.user,limit_n=10)
if (res.status_code != 200):
return 0
@@ -227,6 +254,9 @@ def get_workout(user,stravaid):
if (r.stravatoken == '') or (r.stravatoken is None):
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate):
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
# ready to fetch. Hurray
fetchresolution = 'high'