Skip to content

Commit

Permalink
Add refresh token functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
BubuDavid committed Oct 9, 2021
1 parent 09ea8bb commit 55008f6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions bbfy/bbfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def require_token(method):
def wrapper(ref, *args, **kwargs):
if not ref.has_access_token:
raise Exception('This needs an access token!')
if ref.did_expired_token:
if ref.did_expired_token():
raise Exception('This token already expired!')

return method(ref, *args, **kwargs)
Expand Down Expand Up @@ -143,7 +143,7 @@ def set_token(self, token=None, expiration_time=None):
print('-------------')
print('We can not set the token')
print('-------------')
raise Exception(f'Sorry, no token available: {status}')
raise Exception(f'Sorry, this is not a validate token: {status}')

# Store the token
response = r.json()
Expand Down Expand Up @@ -192,8 +192,12 @@ def refresh_access_token(self):
'grant_type' : 'refresh_token',
'refresh_token': self.__refresh_token,
}
# Get the headers
headers = self.__get_headers_authorization()
# Define headers with the encode base 64 credentials
client_credentials = f'{self.__client_id}:{self.__client_secret}'
client_credentials_b64 = base64.b64encode(client_credentials.encode()).decode()
headers = {
'Authorization' : f'Basic {client_credentials_b64}'
}
# Make post request
r = requests.post(
url=endpoint,
Expand All @@ -206,18 +210,14 @@ def refresh_access_token(self):
print('-------------')
print('We can not set the token from this refreshed token')
print('-------------')
raise Exception(f'Sorry, no token available, for refresh token: {status}')
raise Exception(f'Sorry, not validate token, for refresh token: {status}')

# Store the token
response = r.json()
self.__access_token = response['access_token']
self.has_access_token = True
# Set the expiration
self.set_expiration_time(response)
# Check if did expired
self.set_did_expired()
# Set the refresh token
self.set_refresh_token(response['refresh_token'])


# * Method that request for user data
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
setup(
name = 'bbfy', # How you named your package folder (MyLib)
packages = ['bbfy'], # Chose the same as "name"
version = '0.2', # Start with a small number and increase it with every change you make
version = '0.2.2', # Start with a small number and increase it with every change you make
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
description = 'A simple API made with Python to consume Spotify API for my personal use', # Give a short description about your library
author = 'David Pedroza Segoviano (Bubu)', # Type in your name
author_email = '[email protected]', # Type in your E-Mail
url = 'https://github.com/BubuDavid/Bubufy', # Provide either the link to your github or to your website
download_url = 'https://github.com/BubuDavid/Bubufy/archive/refs/tags/v0.2.tar.gz', # I explain this later on
download_url = 'https://github.com/BubuDavid/Bubufy/archive/refs/tags/v0.2.1.tar.gz', # I explain this later on
keywords = ['Python', 'SpotifyAPI'], # Keywords that define your package best
install_requires=[ # I get to this in a second
'requests',
Expand Down

0 comments on commit 55008f6

Please sign in to comment.