From 55008f6737321b4c29c5cceb67b061680c7c3048 Mon Sep 17 00:00:00 2001 From: BubuDavid Date: Sat, 9 Oct 2021 16:06:45 -0500 Subject: [PATCH] Add refresh token functionality --- bbfy/bbfy.py | 18 +++++++++--------- setup.py | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bbfy/bbfy.py b/bbfy/bbfy.py index 96cd56a..17f4ab1 100644 --- a/bbfy/bbfy.py +++ b/bbfy/bbfy.py @@ -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) @@ -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() @@ -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, @@ -206,7 +210,7 @@ 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() @@ -214,10 +218,6 @@ def refresh_access_token(self): 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 diff --git a/setup.py b/setup.py index c7164a9..3e32e26 100644 --- a/setup.py +++ b/setup.py @@ -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 = 'david.pedroza.segoviano@gmail.com', # 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',