From b71937d0db836b813cacdf01a0e88bb292551f06 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Sat, 22 Apr 2017 19:40:32 +0200 Subject: [PATCH] tested refresh TP token --- rowers/tpstuff.py | 5 +++-- rowers/views.py | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/rowers/tpstuff.py b/rowers/tpstuff.py index 89a3d390..7638f7f2 100644 --- a/rowers/tpstuff.py +++ b/rowers/tpstuff.py @@ -94,7 +94,7 @@ def custom_exception_handler(exc,message): return res # Refresh ST token using refresh token -def do_refresh_token(refreshtoken,access_token): +def do_refresh_token(refreshtoken): client_auth = requests.auth.HTTPBasicAuth(TP_CLIENT_KEY, TP_CLIENT_SECRET) post_data = {"grant_type": "refresh_token", "client_secret": TP_CLIENT_SECRET, @@ -104,7 +104,8 @@ def do_refresh_token(refreshtoken,access_token): headers = {'user-agent': 'sanderroosendaal', 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', - 'authorization': 'Bearer %s' % access_token} + } + url = "https://oauth.sandbox.trainingpeaks.com/oauth/token" diff --git a/rowers/views.py b/rowers/views.py index 6f3f1f61..c87b132f 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -1087,7 +1087,13 @@ def tp_open(user): raise TPNoTokenError("User has no token") else: if (timezone.now()>r.tptokenexpirydate): - thetoken = tpstuff.rower_tp_token_refresh(user) + res = tpstuff.do_refresh_token(r.tprefreshtoken) + r.tptoken = res[0] + r.tprefreshtoken = res[2] + expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1]) + r.tptokenexpirydate = expirydatetime + r.save() + thetoken = r.tptoken else: thetoken = r.tptoken