Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authorization code method #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "2.6"
- "2.7"
install: "pip install -r test-requirements.txt"
script: "make test-coveralls"
Expand Down
46 changes: 46 additions & 0 deletions shapeways/oauth2_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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'

Expand All @@ -47,6 +48,51 @@ def authenticate(self, client_id, client_secret):
print(response.content)
return False

# Oauth2 authentication method
def authenticate_authorization_code(self, client_id, client_secret, authorizaion_code='eBGI9tpcMrBZwD9Yguqz'):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest removing the authorization_code's default value since the code is required by the request and quickly expires (default value becomes obsolete).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

"""
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']
return True

# Internal wrapper functions to make endpoint code easier to read and less repetitive
def _validate_response(self, response):
"""
Expand Down