From a7773a6f4a752650e65e896e00a60ebd67b115a9 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Wed, 17 Aug 2022 02:09:22 +0200 Subject: [PATCH] v2/file now works --- rowers/tpstuff.py | 13 ++++++++----- rowers/views/importviews.py | 7 +++++-- rowsandall_app/settings.py | 5 +++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/rowers/tpstuff.py b/rowers/tpstuff.py index 0ac7187d..1fb29c26 100644 --- a/rowers/tpstuff.py +++ b/rowers/tpstuff.py @@ -17,7 +17,8 @@ from rowsandall_app.settings import ( C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET, STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET, TP_CLIENT_ID, TP_CLIENT_SECRET, - TP_REDIRECT_URI, TP_CLIENT_KEY,TP_API_LOCATION + TP_REDIRECT_URI, TP_CLIENT_KEY,TP_API_LOCATION, + TP_OAUTH_LOCATION, ) tpapilocation = TP_API_LOCATION @@ -50,7 +51,6 @@ def do_refresh_token(refreshtoken): # Exchange access code for long-lived access token - def get_token(code): # client_auth = requests.auth.HTTPBasicAuth(TP_CLIENT_KEY, TP_CLIENT_SECRET) post_data = { @@ -62,10 +62,13 @@ def get_token(code): } response = requests.post( - "https://oauth.trainingpeaks.com/oauth/token", + TP_OAUTH_LOCATION+"/oauth/token/", data=post_data, verify=False, ) + if response.status_code != 200: + return 0,0,0 + try: token_json = response.json() thetoken = token_json['access_token'] @@ -116,7 +119,7 @@ def tp_check(access_token): # pragma: no cover 'authorization': 'Bearer %s' % access_token } - resp = requests.post(tpapilocation+"/v1/info/version", + resp = requests.post(tpapilocation+"/v2/info/version", headers=headers, verify=False) return resp @@ -147,7 +150,7 @@ def uploadactivity(access_token, filename, description='', "Data": base64.b64encode(data_gz.getvalue()).decode("ascii") } - resp = requests.post(tpapilocation+"/v1/file", + resp = requests.post(tpapilocation+"/v2/file/synchronous", data=json.dumps(data), headers=headers, verify=False) diff --git a/rowers/views/importviews.py b/rowers/views/importviews.py index 82e13b48..c34d83d4 100644 --- a/rowers/views/importviews.py +++ b/rowers/views/importviews.py @@ -1,4 +1,7 @@ -from rowsandall_app.settings import NK_OAUTH_LOCATION, ROJABO_OAUTH_LOCATION +from rowsandall_app.settings import ( + NK_OAUTH_LOCATION, ROJABO_OAUTH_LOCATION, + TP_OAUTH_LOCATION, + ) from rowers.views.statements import * from rowers.plannedsessions import get_dates_timeperiod @@ -295,7 +298,7 @@ def rower_tp_authorize(request): # pragma: no cover "redirect_uri": TP_REDIRECT_URI, "scope": "file:write", } - url = "https://oauth.trainingpeaks.com/oauth/authorize/?" + \ + url = TP_OAUTH_LOCATION+"oauth/authorize/?" + \ urllib.parse.urlencode(params) return HttpResponseRedirect(url) diff --git a/rowsandall_app/settings.py b/rowsandall_app/settings.py index 3ee00411..ad7eb610 100644 --- a/rowsandall_app/settings.py +++ b/rowsandall_app/settings.py @@ -328,6 +328,11 @@ try: except KeyError: TP_API_LOCATION = "https://api.trainingpeaks.com" +try: + TP_OAUTH_LOCATION = CFG['tp_oauth_location'] +except KeyError: + TP_OAUTH_LOCATION = "https://oauth.trainingpeaks.com/oauth/token" + # RP3 RP3_CLIENT_ID = CFG["rp3_client_id"] RP3_CLIENT_SECRET = CFG["rp3_client_secret"]