From d1c5f57dad6013e4d615aab95d62e705eca3dc95 Mon Sep 17 00:00:00 2001 From: Nlb Date: Wed, 14 Mar 2018 17:45:00 -0400 Subject: [PATCH] adding refresh token --- .gitignore | 1 + quickbooks/auth.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/.gitignore b/.gitignore index c0dc101..6307ae2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ __pycache__/ *.py[cod] *$py.class .idea/* +.vscode/* # C extensions *.so diff --git a/quickbooks/auth.py b/quickbooks/auth.py index f23f02e..7f289eb 100644 --- a/quickbooks/auth.py +++ b/quickbooks/auth.py @@ -261,3 +261,30 @@ def get_auth_header(self): auth_header = base64.b64encode(bytes(self.client_id + ':' + self.client_secret, 'utf-8')).decode() return 'Basic ' + auth_header + + def get_new_access_tokens(self, refresh_token): + + headers = { + 'Accept': 'application/json', + 'content-type': 'application/x-www-form-urlencoded', + 'Authorization': self.get_auth_header() + } + + payload = { + 'refresh_token': refresh_token, + 'grant_type': 'refresh_token' + } + + response = requests.post(self.access_token_url, data=payload, headers=headers) + if response.status_code != 200: + return response.text + + bearer_raw = json.loads(response.text) + self.x_refresh_token_expires_in = bearer_raw['x_refresh_token_expires_in'] + self.access_token = bearer_raw['access_token'] + self.token_type = bearer_raw['token_type'] + self.refresh_token = bearer_raw['refresh_token'] + self.expires_in = bearer_raw['expires_in'] + + if 'id_token' in bearer_raw: + self.id_token = bearer_raw['id_token']