From d631c254015c382443c874d51edce71e5f7f2770 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 18 Nov 2019 09:30:18 -0500 Subject: [PATCH 1/3] Add refresh token --- shapeways/oauth2_client.py | 49 +++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/shapeways/oauth2_client.py b/shapeways/oauth2_client.py index 59d7c02..3ab1453 100644 --- a/shapeways/oauth2_client.py +++ b/shapeways/oauth2_client.py @@ -21,10 +21,11 @@ class ShapewaysOauth2Client(): def __init__(self, api_url=None): self.access_token = None + self.refresh_token = None if not api_url: self.api_url = 'https://api.shapeways.com' - # Oauth2 authentication method + # Oauth2 authentication method Y11nbjdGeBpIVtUpPvdH def authenticate(self, client_id, client_secret): """ Authenticate your application and retrieve a bearer token @@ -47,6 +48,52 @@ def authenticate(self, client_id, client_secret): print(response.content) return False + # Oauth2 authentication method Y11nbjdGeBpIVtUpPvdH + def authenticate_authorization_code(self, client_id, client_secret, authorizaion_code='eBGI9tpcMrBZwD9Yguqz'): + """ + Authenticate your application and retrieve a bearer token + + :type client_id: str + :type client_secret: str + :return: True for success, false for Failure + :rtype: bool + """ + auth_post_data = { + 'grant_type': 'authorization_code', + 'code': authorizaion_code, + 'client_id': client_id, + 'client_secret': client_secret, + 'redirect_uri': 'oob' + } + + response = requests.post(url=self.api_url + AUTH_URL, data=auth_post_data) + + if response.status_code == 200: + self.access_token = response.json()['access_token'] + self.refresh_token = response.json()['refresh_token'] + return True + print("Error: status code " + str(response.status_code)) + print(response.content) + return False + + def refresh_access(self, client_id, client_secret, refresh_token): + + headers = { + 'Authorization': 'Basic ' + client_secret + } + + refresh_post_data = { + 'grant_type': 'refresh_token', + 'client_id': client_id, + 'refresh_token': refresh_token + } + + response = requests.post(url=self.api_url + AUTH_URL, headers=headers, data=refresh_post_data) + if response.status_code == 200: + self.access_token = response.json()['access_token'] + self.refresh_token = response.json()['refresh_token'] + return True + # Internal wrapper functions to make endpoint code easier to read and less repetitive def _validate_response(self, response): """ From d4353c45d019c7dfcc56b769ef24a499af59678e Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 18 Nov 2019 09:31:53 -0500 Subject: [PATCH 2/3] Adjust --- shapeways/oauth2_client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shapeways/oauth2_client.py b/shapeways/oauth2_client.py index 3ab1453..b1edaf4 100644 --- a/shapeways/oauth2_client.py +++ b/shapeways/oauth2_client.py @@ -48,7 +48,7 @@ def authenticate(self, client_id, client_secret): print(response.content) return False - # Oauth2 authentication method Y11nbjdGeBpIVtUpPvdH + # Oauth2 authentication method def authenticate_authorization_code(self, client_id, client_secret, authorizaion_code='eBGI9tpcMrBZwD9Yguqz'): """ Authenticate your application and retrieve a bearer token @@ -91,7 +91,6 @@ def refresh_access(self, client_id, client_secret, refresh_token): response = requests.post(url=self.api_url + AUTH_URL, headers=headers, data=refresh_post_data) if response.status_code == 200: self.access_token = response.json()['access_token'] - self.refresh_token = response.json()['refresh_token'] return True # Internal wrapper functions to make endpoint code easier to read and less repetitive From f7db148c23daf3148688b616f1ce3ab58428d70f Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 18 Nov 2019 09:44:12 -0500 Subject: [PATCH 3/3] We don't support 2.6 --- .travis.yml | 1 - shapeways/oauth2_client.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f38f3ce..180fc0f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: python python: - - "2.6" - "2.7" install: "pip install -r test-requirements.txt" script: "make test-coveralls" diff --git a/shapeways/oauth2_client.py b/shapeways/oauth2_client.py index b1edaf4..fa9a862 100644 --- a/shapeways/oauth2_client.py +++ b/shapeways/oauth2_client.py @@ -25,7 +25,7 @@ def __init__(self, api_url=None): if not api_url: self.api_url = 'https://api.shapeways.com' - # Oauth2 authentication method Y11nbjdGeBpIVtUpPvdH + # Oauth2 authentication method def authenticate(self, client_id, client_secret): """ Authenticate your application and retrieve a bearer token