def get_session_using_saver(saver: Saver, sess: Union[OAuth2Service, OAuth1Service], code: str, default_expiration_time_second: int = 3600, name: str = "") \ -> Tuple[Union[OAuth2Service, OAuth1Service], Union[str, None], Union[str, None], Union[str, None], Union[str, int, None]]: global args def global_decoder(payload): """ Decoder for global API :param payload: The payload to decode :return: Return a dictionary that contains all data given by the API """ results = json.loads(payload.decode('utf-8')) if "access_token" in results.keys(): saver["access_token"] = results["access_token"] saver["access_token_timestamp"] = int(time.time()) if args.debug: print("{} access token = {}".format(name, results["access_token"])) if "refresh_token" in results.keys(): saver["refresh_token"] = results["refresh_token"] if "token_type" in results.keys(): saver["token_type"] = results["token_type"] if "expires_in" in results.keys(): saver["expires_in"] = results["expires_in"] return results # If the access and refresh tokens are already saved, use them if {"access_token", "access_token_timestamp", "token_type"}.issubset( saver.keys()) and saver.get("access_token_timestamp", None) is not None and int(time.time()) - \ int(saver["access_token_timestamp"]) <= int(saver.get("expires_in", default_expiration_time_second)): access_token = saver["access_token"] if args.debug: print("{} access token = {}".format(name, access_token)) if "refresh_token" in saver.keys(): refresh_token = saver["refresh_token"] else: refresh_token = None token_type = saver["token_type"] if "expires_in" in saver.keys(): expires_in = saver["expires_in"] else: expires_in = None session = sess.get_session(token=access_token) else: access_token = refresh_token = token_type = expires_in = None data = { "code": code, "grant_type": "authorization_code", "redirect_uri": redirect_uri } if args.debug: print("{} payload = {}".format(name, data)) session = sess.get_auth_session(data=data, decoder=global_decoder) return session, access_token, refresh_token, token_type, expires_in
# Get a spotify_saver to save all the credentials to save time spotify_saver = Saver(".cache-rauth-spotify.json") # Create the OAuth2 service instance for Spotify API spotify = OAuth2Service( client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET, name="spotify", authorize_url="https://accounts.spotify.com/authorize", access_token_url="https://accounts.spotify.com/api/token", base_url="https://api.spotify.com/v1/", ) # Search if there is a save: if spotify_saver.get("code", None) is not None and spotify_saver.get("code_timestamp", None) is not None and \ int(time.time()) - int(spotify_saver["code_timestamp"]) <= 3600: spotify_code = str(spotify_saver["code"]) else: # Connect to the Spotify API to get the authentication code params = { "scope": "user-read-currently-playing", "response_type": "code", "redirect_uri": redirect_uri } # Launch server e = Event() def launch_after(e: Event): """ Open the authorize URL to the Spotify API through a new tab in the default browser.