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,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"]