workout list nk import working
This commit is contained in:
@@ -53,51 +53,84 @@ oauth_data = {
|
||||
'scope':'read',
|
||||
}
|
||||
|
||||
import http.client as http_client
|
||||
http_client.HTTPConnection.debuglevel = 1
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
def get_token(code,callbackuri):
|
||||
#client_id = oauth_data['client_id']
|
||||
#client_secret = oauth_data['client_secret']
|
||||
#base_uri = oauth_data['base_url']
|
||||
|
||||
#nk = OAuth2Session(client_id)
|
||||
#token = nk.fetch_token(base_uri,client_secret=client_secret,authorization_response=callbackuri)
|
||||
|
||||
#print(token)
|
||||
#return [0,0,0]
|
||||
def get_token(code):
|
||||
url = oauth_data['base_url']
|
||||
|
||||
|
||||
headers = {'Accept': 'application/json',
|
||||
#'Api-Key': oauth_data['client_id'],
|
||||
#'Content-Type': 'application/x-www-form-urlencoded',
|
||||
#'Authorization': auth_header,
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
#'user-agent': 'sanderroosendaal'
|
||||
}
|
||||
|
||||
|
||||
|
||||
post_data = {"client_id": oauth_data['client_id'],
|
||||
"grant_type": "authorization_code",
|
||||
"redirect_uri": oauth_data['redirect_uri'],
|
||||
"client_secret": oauth_data['client_secret'],
|
||||
"code": code,
|
||||
#"response_type": "code",
|
||||
# "scope":oauth_data['scope'],
|
||||
}
|
||||
|
||||
response = requests.post(url,auth=HTTPBasicAuth(oauth_data['client_id'],oauth_data['client_secret']),
|
||||
data=post_data)
|
||||
|
||||
if response.status_code != 200:
|
||||
return [0,response.text,0,0]
|
||||
|
||||
token_json = response.json()
|
||||
|
||||
access_token = token_json['access_token']
|
||||
refresh_token = token_json['refresh_token']
|
||||
expires_in = token_json['expires_in']
|
||||
nk_owner_id = token_json['user_id']
|
||||
|
||||
return [access_token, expires_in, refresh_token,nk_owner_id]
|
||||
|
||||
response = requests.post(url,data=json.dumps(post_data),headers=headers)
|
||||
print('Status Code',response.status_code)
|
||||
return [0,response.text,0]
|
||||
#return imports_get_token(code, oauth_data)
|
||||
|
||||
def nk_open(user):
|
||||
t = time.localtime()
|
||||
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
|
||||
token = imports_open(user,oauth_data)
|
||||
return token
|
||||
r = Rower.objects.get(user=user)
|
||||
print(r.nktokenexpirydate)
|
||||
if (r.nktoken == '') or (r.nktoken is None):
|
||||
s = "Token doesn't exist. Need to authorize"
|
||||
raise NoTokenError("User has no token")
|
||||
else:
|
||||
if (timezone.now()>r.nktokenexpirydate):
|
||||
thetoken = rower_nk_token_refresh(user)
|
||||
if thetoken == None:
|
||||
raise NoTokenError("User has no token")
|
||||
return thetoken
|
||||
else:
|
||||
thetoken = r.nktoken
|
||||
|
||||
return thetoken
|
||||
|
||||
def do_refresh_token(refreshtoken):
|
||||
return imports_do_refresh_token(refreshtoken, oauth_data)
|
||||
post_data = {"grant_type": "refresh_token",
|
||||
#"client_id":NK_CLIENT_ID,
|
||||
"refresh_token": refreshtoken,
|
||||
}
|
||||
|
||||
url = oauth_data['base_url']
|
||||
|
||||
response = requests.post(url,data=post_data,auth=HTTPBasicAuth(oauth_data['client_id'],oauth_data['client_secret']))
|
||||
|
||||
print(response.request.headers)
|
||||
print(post_data)
|
||||
print(response.status_code,response.text)
|
||||
|
||||
if response.status_code != 200:
|
||||
return [0,0,0]
|
||||
|
||||
token_json = response.json()
|
||||
|
||||
access_token = token_json['access_token']
|
||||
refresh_token = token_json['refresh_token']
|
||||
expires_in = token_json['expires_in']
|
||||
|
||||
return access_token, expires_in, refresh_token
|
||||
|
||||
|
||||
def rower_nk_token_refresh(user):
|
||||
r = Rower.objects.get(user=user)
|
||||
@@ -128,16 +161,25 @@ def get_nk_workout_list(user,fake=False):
|
||||
return custom_exception_handler(401,s)
|
||||
else:
|
||||
# ready to fetch. Hurray
|
||||
endTime = int(arrow.now().timestamp())*1000
|
||||
endTime = str(endTime)
|
||||
startTime = arrow.now()-timedelta(days=90)*1000
|
||||
startTime = str(int(startTime.timestamp()))
|
||||
authorizationstring = str('Bearer ' + r.nktoken)
|
||||
headers = {'Authorization': authorizationstring,
|
||||
'user-agent': 'sanderroosendaal',
|
||||
'Content-Type': 'application/json'}
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
|
||||
url = NK_API_LOCATION+"api/v1/sessions"
|
||||
|
||||
params = {} # start / end time
|
||||
params = {
|
||||
'startTime':startTime,
|
||||
'endTime':endTime,
|
||||
} # start / end time
|
||||
|
||||
s = requests.get(url,headers=headers,params=params)
|
||||
#print(s.status_code,s.json())
|
||||
|
||||
return s
|
||||
|
||||
|
||||
Reference in New Issue
Block a user