Private
Public Access
1
0

Merge tag 'v7.27' into develop

repairing trainingpeaks sync
This commit is contained in:
Sander Roosendaal
2018-07-09 06:41:29 +02:00
3 changed files with 33 additions and 5 deletions

View File

@@ -100,6 +100,7 @@ def imports_open(user,oauth_data):
tokenname,
refreshtokenname,
expirydatename,
oauth_data,
)
return token
@@ -190,6 +191,7 @@ def imports_get_token(
data=post_data,
headers=headers)
if response.status_code == 200 or response.status_code == 201:
token_json = response.json()
thetoken = token_json['access_token']
@@ -231,12 +233,12 @@ def imports_make_authorization_url(oauth_data):
return HttpResponseRedirect(url)
# This is token refresh. Looks for tokens in our database, then refreshes
def imports_token_refresh(user,tokenname,refreshtokenname,expirydatename):
def imports_token_refresh(user,tokenname,refreshtokenname,expirydatename,oauth_data):
r = Rower.objects.get(user=user)
refreshtoken = getattr(r,refreshtokennname)
refreshtoken = getattr(r,refreshtokenname)
res = imports_do_refresh_token(refreshtoken)
res = imports_do_refresh_token(refreshtoken,oauth_data)
access_token = res[0]
expires_in = res[1]
refresh_token = res[2]

View File

@@ -28,6 +28,7 @@ oauth_data = {
'redirect_uri': TP_REDIRECT_URI,
'autorization_uri': "https://oauth.trainingpeaks.com/oauth/authorize?",
'content_type': 'application/x-www-form-urlencoded',
# 'content_type': 'application/json',
'tokenname': 'tptoken',
'refreshtokenname': 'tprefreshtoken',
'expirydatename': 'tptokenexpirydate',
@@ -46,7 +47,33 @@ def do_refresh_token(refreshtoken):
# Exchange access code for long-lived access token
def get_token(code):
return imports_get_token(code, oauth_data)
client_auth = requests.auth.HTTPBasicAuth(TP_CLIENT_KEY, TP_CLIENT_SECRET)
post_data = {
"client_id":TP_CLIENT_KEY,
"grant_type": "authorization_code",
"code": code,
"redirect_uri":TP_REDIRECT_URI,
"client_secret": TP_CLIENT_SECRET,
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
response = requests.post("https://oauth.trainingpeaks.com/oauth/token",
data=post_data)
try:
token_json = response.json()
thetoken = token_json['access_token']
expires_in = token_json['expires_in']
refresh_token = token_json['refresh_token']
except KeyError:
thetoken = 0
expires_in = 0
refresh_token = 0
return thetoken,expires_in,refresh_token
# Make authorization URL including random string
def make_authorization_url(request):

View File

@@ -2294,7 +2294,6 @@ def rower_process_tpcallback(request):
code = request.GET['code']
res = tpstuff.get_token(code)
access_token = res[0]
expires_in = res[1]
refresh_token = res[2]