Private
Public Access
1
0

more sofisticated TP authorization/refresh

This commit is contained in:
Sander Roosendaal
2017-04-25 17:49:46 +02:00
parent 94c8759c9b
commit 3e21fd9175
2 changed files with 29 additions and 24 deletions

View File

@@ -113,6 +113,7 @@ def do_refresh_token(refreshtoken):
data=post_data, data=post_data,
headers=headers) headers=headers)
if response.status_code == 200:
token_json = response.json() token_json = response.json()
thetoken = token_json['access_token'] thetoken = token_json['access_token']
expires_in = token_json['expires_in'] expires_in = token_json['expires_in']
@@ -120,6 +121,9 @@ def do_refresh_token(refreshtoken):
refresh_token = token_json['refresh_token'] refresh_token = token_json['refresh_token']
except KeyError: except KeyError:
refresh_token = refreshtoken refresh_token = refreshtoken
else:
return [0,0,0]
return [thetoken,expires_in,refresh_token] return [thetoken,expires_in,refresh_token]
@@ -184,10 +188,6 @@ def createtpworkoutdata(w):
row = rowingdata(filename) row = rowingdata(filename)
tcxfilename = filename[:-4]+'.tcx' tcxfilename = filename[:-4]+'.tcx'
row.exporttotcx(tcxfilename,notes=w.notes) row.exporttotcx(tcxfilename,notes=w.notes)
# with file(tcxfilename,'rb') as inF:
# s = inF.read()
# with gzip.GzipFile(tcxfilename+'.gz','wb') as outF:
# outF.write(s)
return tcxfilename return tcxfilename
except: except:
tcxfilename = 0 tcxfilename = 0
@@ -208,12 +208,14 @@ def tp_check(access_token):
def uploadactivity(access_token,filename,description='', def uploadactivity(access_token,filename,description='',
name='Rowsandall.com workout'): name='Rowsandall.com workout'):
data_gz = BytesIO() data_gz = BytesIO()
with file(filename,'rb') as inF: with file(filename,'rb') as inF:
s = inF.read() s = inF.read()
with gzip.GzipFile(fileobj=data_gz,mode="w") as gzf: with gzip.GzipFile(fileobj=data_gz,mode="w") as gzf:
gzf.write(s) gzf.write(s)
headers = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
'Accept': 'application/json', 'Accept': 'application/json',
@@ -221,6 +223,7 @@ def uploadactivity(access_token,filename,description='',
} }
data = { data = {
"UploadClient": "rowsandall", "UploadClient": "rowsandall",
"Filename": filename, "Filename": filename,
@@ -235,8 +238,8 @@ def uploadactivity(access_token,filename,description='',
data = json.dumps(data), data = json.dumps(data),
headers=headers) headers=headers)
if resp.status_code != 200: if resp.status_code != 200:
if settings.DEBUG:
print resp.status_code print resp.status_code
print resp.reason print resp.reason
print "" print ""

View File

@@ -1088,12 +1088,15 @@ def tp_open(user):
else: else:
if (timezone.now()>r.tptokenexpirydate): if (timezone.now()>r.tptokenexpirydate):
res = tpstuff.do_refresh_token(r.tprefreshtoken) res = tpstuff.do_refresh_token(r.tprefreshtoken)
if res[0] != 0:
r.tptoken = res[0] r.tptoken = res[0]
r.tprefreshtoken = res[2] r.tprefreshtoken = res[2]
expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1]) expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1])
r.tptokenexpirydate = expirydatetime r.tptokenexpirydate = expirydatetime
r.save() r.save()
thetoken = r.tptoken thetoken = r.tptoken
else:
raise TPNoTokenError("Refresh token invalid")
else: else:
thetoken = r.tptoken thetoken = r.tptoken
@@ -1225,7 +1228,6 @@ def workout_tp_upload_view(request,id=0):
except TPNoTokenError: except TPNoTokenError:
return HttpResponseRedirect("/rowers/me/tpauthorize/") return HttpResponseRedirect("/rowers/me/tpauthorize/")
# ready to upload. Hurray # ready to upload. Hurray
try: try:
w = Workout.objects.get(id=id) w = Workout.objects.get(id=id)