Commit 754b1cdc authored by Cédrick COPOL's avatar Cédrick COPOL

Ajoute un peu de doc

parent 784d6b9e
app.json
withings/tokens.json
withings/__pycache__/
......@@ -16,7 +16,7 @@ class Token(object):
oauth_token = "/oauth2/token/?grant_type=authorization_code"
oauth2_token = "/oauth2/token" # attention ne pas mettre de '/' à la fin
def __init__(self, appIDFile, backup=root/"tokens.json", mode=None) :
def __init__(self, appIDFile, backup=root/"tokens.json", mode=None, clean=False) :
"""
Parameters
----------
......@@ -29,6 +29,8 @@ class Token(object):
Please, create your app at https://account.withings.com/partner/dashboard_oauth2
mode: optional
Set to `demo` if you want to use the demo user of Withings.
clean: boolean
If True, clean the backup file and ask for a new authentification code
"""
with open(appIDFile, "r") as txt:
appID = json.load(txt)
......@@ -42,12 +44,14 @@ class Token(object):
self.userid = None
self.birthday = None
self.backup_file = backup
if self.backup_file.is_file():
if self.backup_file.is_file() and not clean:
self._read()
if self._refresh_me():
self.refresh()
print("Token has been refreshed")
else:
if self.backup_file.is_file():
self.backup_file.unlink()
print("Click on this link to get your authentification code")
print(self._authorize_link(mode))
......@@ -122,12 +126,9 @@ class Token(object):
self.token = r.json()
self.userid = self.token["userid"]
self._write()
return self.token
def refresh(self):
"""Refresh credentials"""
# Important: Parameters must be set in the body request and not in the url.
# ---------
data = {
"client_id" : self.id,
"client_secret": self.secret,
......@@ -139,14 +140,12 @@ class Token(object):
self.birthday = datetime.timestamp(datetime.now())
# Important: Parameters must be set in the body request and not in the url.
# ---------
# It's why we use `data` and note `params`
# It's why we use `data` and not `params`
r = requests.post(url, data=data)
if r.status_code != 200 or "errors" in r.json():
if r.status_code != 200:
self.birthday = None
print("status", r.status_code, r.json())
raise Exception(f"Impossible to get credentials. status: {r.status_code}. error: {r.json()['errors']}")
raise Exception(f"Impossible to refresh credentials. status: {r.status_code}. error: {r.json()['errors'][0]['message']}")
self.token = r.json()
self.userid = self.token["userid"]
self._write()
return self.token
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment