more sofisticated TP authorization/refresh
This commit is contained in:
@@ -113,14 +113,18 @@ def do_refresh_token(refreshtoken):
|
|||||||
data=post_data,
|
data=post_data,
|
||||||
headers=headers)
|
headers=headers)
|
||||||
|
|
||||||
token_json = response.json()
|
if response.status_code == 200:
|
||||||
thetoken = token_json['access_token']
|
token_json = response.json()
|
||||||
expires_in = token_json['expires_in']
|
thetoken = token_json['access_token']
|
||||||
try:
|
expires_in = token_json['expires_in']
|
||||||
refresh_token = token_json['refresh_token']
|
try:
|
||||||
except KeyError:
|
refresh_token = token_json['refresh_token']
|
||||||
refresh_token = refreshtoken
|
except KeyError:
|
||||||
|
refresh_token = refreshtoken
|
||||||
|
else:
|
||||||
|
return [0,0,0]
|
||||||
|
|
||||||
|
|
||||||
return [thetoken,expires_in,refresh_token]
|
return [thetoken,expires_in,refresh_token]
|
||||||
|
|
||||||
# Exchange access code for long-lived access token
|
# Exchange access code for long-lived access 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,13 +238,13 @@ 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:
|
||||||
print resp.status_code
|
if settings.DEBUG:
|
||||||
print resp.reason
|
print resp.status_code
|
||||||
print ""
|
print resp.reason
|
||||||
print headers
|
print ""
|
||||||
print ""
|
print headers
|
||||||
|
print ""
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
return resp.json()[0]["Id"]
|
return resp.json()[0]["Id"]
|
||||||
|
|||||||
@@ -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)
|
||||||
r.tptoken = res[0]
|
if res[0] != 0:
|
||||||
r.tprefreshtoken = res[2]
|
r.tptoken = res[0]
|
||||||
expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1])
|
r.tprefreshtoken = res[2]
|
||||||
r.tptokenexpirydate = expirydatetime
|
expirydatetime = timezone.now()+datetime.timedelta(seconds=res[1])
|
||||||
r.save()
|
r.tptokenexpirydate = expirydatetime
|
||||||
thetoken = r.tptoken
|
r.save()
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user