platypush/common/spotify/__init__.py
def spotify_user_authenticate(self, scopes: Optional[Iterable[str]] = None):
        """
        Authenticate to the Spotify API for requests that require access to user data.
        """
        if self._spotify_user_token:
            return

        try:
            self._spotify_load_user_credentials(scopes=scopes or self.spotify_required_scopes)
            if self._spotify_user_token:
                return

            if self._spotify_refresh_token:
                try:
                    self._spotify_refresh_user_token()
                    return
                except Exception as e:
                    self._spotify_logger.error(f'Unable to refresh the user access token: {e}')
        except MissingScopesException as e:
            self._spotify_logger.warning(e)

        http = get_backend('http')
        assert http, 'HTTP backend not configured'
        callback_url = '{scheme}://{host}:{port}/spotify/auth_callback'.format(
            scheme="https" if http.ssl_context else "http",
            host=get_ip_or_hostname(),
            port=http.port,
        )

        state = b64encode(bytes([randint(0, 255) for _ in range(18)])).decode()
        self._spotify_logger.warning('\n\nUnauthenticated Spotify session or scopes not provided by the user. Please '
                                     'open the following URL in a browser to authenticate:\n'
                                     'https://accounts.spotify.com/authorize?client_id='
                                     f'{self._spotify_api_credentials[0]}&'
                                     f'response_type=code&redirect_uri={parse.quote(callback_url, safe="")}'
                                     f'&scope={parse.quote(" ".join(scopes))}&state={state}.\n'
                                     'Replace the host in the callback URL with the IP/hostname of this machine '
                                     f'accessible to your browser if required, and make sure to add {callback_url} '
                                     'to the list of whitelisted callbacks on your Spotify application page.\n')

        redis = get_redis()
        msg = json.loads(redis.blpop(self.get_spotify_queue_for_state(state))[1].decode())
        assert not msg.get('error'), f'Authentication error: {msg["error"]}'
        self._spotify_user_authenticate_phase_2(code=msg['code'], callback_url=callback_url, scopes=scopes)

        Authenticate to the Spotify API for requests that require access to user data.
        
Authenticate to the Spotify API for requests that require access to user data.
Authenticate to the Spotify API for requests that require access to user data .
def spotify_user_authenticate(self, scopes: Optional[Iterable[str]] = None):
        if self._spotify_user_token:
            return
        try:
            self._spotify_load_user_credentials(scopes=scopes or self.spotify_required_scopes)
            if self._spotify_user_token:
                return
            if self._spotify_refresh_token:
                try:
                    self._spotify_refresh_user_token()
                    return
                except Exception as e:
                    self._spotify_logger.error(f'Unable to refresh the user access token: {e}')
        except MissingScopesException as e:
            self._spotify_logger.warning(e)
        http = get_backend('http')
        assert http, 'HTTP backend not configured'
        callback_url = '{scheme}://{host}:{port}/spotify/auth_callback'.format(
            scheme="https" if http.ssl_context else "http",
            host=get_ip_or_hostname(),
            port=http.port,
        )
        state = b64encode(bytes([randint(0, 255) for _ in range(18)])).decode()
        self._spotify_logger.warning('\n\nUnauthenticated Spotify session or scopes not provided by the user. Please '
                                     'open the following URL in a browser to authenticate:\n'
                                     'https://accounts.spotify.com/authorize?client_id='
                                     f'{self._spotify_api_credentials[0]}&'
                                     f'response_type=code&redirect_uri={parse.quote(callback_url, safe="")}'
                                     f'&scope={parse.quote(" ".join(scopes))}&state={state}.\n'
                                     'Replace the host in the callback URL with the IP/hostname of this machine '
                                     f'accessible to your browser if required, and make sure to add {callback_url} '
                                     'to the list of whitelisted callbacks on your Spotify application page.\n')
        redis = get_redis()
        msg = json.loads(redis.blpop(self.get_spotify_queue_for_state(state))[1].decode())
        assert not msg.get('error'), f'Authentication error: {msg["error"]}'
        self._spotify_user_authenticate_phase_2(code=msg['code'], callback_url=callback_url, scopes=scopes)
def spotify_user_authenticate ( self , scopes : Optional [ Iterable [ str ] ] = None ) : if self . _spotify_user_token : return try : self . _spotify_load_user_credentials ( scopes = scopes or self . spotify_required_scopes ) if self . _spotify_user_token : return if self . _spotify_refresh_token : try : self . _spotify_refresh_user_token ( ) return except Exception as e : self . _spotify_logger . error ( f'Unable to refresh the user access token: {e}' ) except MissingScopesException as e : self . _spotify_logger . warning ( e ) http = get_backend ( 'http' ) assert http , 'HTTP backend not configured' callback_url = '{scheme}://{host}:{port}/spotify/auth_callback' . format ( scheme = "https" if http . ssl_context else "http" , host = get_ip_or_hostname ( ) , port = http . port , ) state = b64encode ( bytes ( [ randint ( 0 , 255 ) for _ in range ( 18 ) ] ) ) . decode ( ) self . _spotify_logger . warning ( '\n\nUnauthenticated Spotify session or scopes not provided by the user. Please ' 'open the following URL in a browser to authenticate:\n' 'https://accounts.spotify.com/authorize?client_id=' f'{self._spotify_api_credentials[0]}&' f'response_type=code&redirect_uri={parse.quote(callback_url, safe="")}' f'&scope={parse.quote(" ".join(scopes))}&state={state}.\n' 'Replace the host in the callback URL with the IP/hostname of this machine ' f'accessible to your browser if required, and make sure to add {callback_url} ' 'to the list of whitelisted callbacks on your Spotify application page.\n' ) redis = get_redis ( ) msg = json . loads ( redis . blpop ( self . get_spotify_queue_for_state ( state ) ) [ 1 ] . decode ( ) ) assert not msg . get ( 'error' ) , f'Authentication error: {msg["error"]}' self . _spotify_user_authenticate_phase_2 ( code = msg [ 'code' ] , callback_url = callback_url , scopes = scopes )
Authenticate to the Spotify API for requests that require access to user data.
Authenticate to the Spotify API for requests that require access to user data .
"""
        Authenticate to the Spotify API for requests that require access to user data.
        """
self
scopes
Optional[Iterable[str]]