more sofisticated TP authorization/refresh
This commit is contained in:
@@ -113,14 +113,18 @@ def do_refresh_token(refreshtoken):
|
||||
data=post_data,
|
||||
headers=headers)
|
||||
|
||||
token_json = response.json()
|
||||
thetoken = token_json['access_token']
|
||||
expires_in = token_json['expires_in']
|
||||
try:
|
||||
refresh_token = token_json['refresh_token']
|
||||
except KeyError:
|
||||
refresh_token = refreshtoken
|
||||
if response.status_code == 200:
|
||||
token_json = response.json()
|
||||
thetoken = token_json['access_token']
|
||||
expires_in = token_json['expires_in']
|
||||
try:
|
||||
refresh_token = token_json['refresh_token']
|
||||
except KeyError:
|
||||
refresh_token = refreshtoken
|
||||
else:
|
||||
return [0,0,0]
|
||||
|
||||
|
||||
return [thetoken,expires_in,refresh_token]
|
||||
|
||||
# Exchange access code for long-lived access token
|
||||
@@ -184,10 +188,6 @@ def createtpworkoutdata(w):
|
||||
row = rowingdata(filename)
|
||||
tcxfilename = filename[:-4]+'.tcx'
|
||||
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
|
||||
except:
|
||||
tcxfilename = 0
|
||||
@@ -208,12 +208,14 @@ def tp_check(access_token):
|
||||
|
||||
def uploadactivity(access_token,filename,description='',
|
||||
name='Rowsandall.com workout'):
|
||||
|
||||
data_gz = BytesIO()
|
||||
with file(filename,'rb') as inF:
|
||||
s = inF.read()
|
||||
with gzip.GzipFile(fileobj=data_gz,mode="w") as gzf:
|
||||
gzf.write(s)
|
||||
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
'Accept': 'application/json',
|
||||
@@ -221,6 +223,7 @@ def uploadactivity(access_token,filename,description='',
|
||||
}
|
||||
|
||||
|
||||
|
||||
data = {
|
||||
"UploadClient": "rowsandall",
|
||||
"Filename": filename,
|
||||
@@ -235,13 +238,13 @@ def uploadactivity(access_token,filename,description='',
|
||||
data = json.dumps(data),
|
||||
headers=headers)
|
||||
|
||||
|
||||
if resp.status_code != 200:
|
||||
print resp.status_code
|
||||
print resp.reason
|
||||
print ""
|
||||
print headers
|
||||
print ""
|
||||
if settings.DEBUG:
|
||||
print resp.status_code
|
||||
print resp.reason
|
||||
print ""
|
||||
print headers
|
||||
print ""
|
||||
return 0
|
||||
else:
|
||||
return resp.json()[0]["Id"]
|
||||
|
||||
@@ -1088,12 +1088,15 @@ def tp_open(user):
|
||||
else:
|
||||
if (timezone.now()>r.tptokenexpirydate):
|
||||
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
|
||||
if res[0] != 0:
|
||||
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:
|
||||
raise TPNoTokenError("Refresh token invalid")
|
||||
else:
|
||||
thetoken = r.tptoken
|
||||
|
||||
@@ -1225,7 +1228,6 @@ def workout_tp_upload_view(request,id=0):
|
||||
except TPNoTokenError:
|
||||
return HttpResponseRedirect("/rowers/me/tpauthorize/")
|
||||
|
||||
|
||||
# ready to upload. Hurray
|
||||
try:
|
||||
w = Workout.objects.get(id=id)
|
||||
|
||||
Reference in New Issue
Block a user