Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_authorize_url_passes_state_from_constructor(self):
state = "STATE"
oauth = SpotifyOAuth("CLID", "CLISEC", "REDIR", state)
url = oauth.get_authorize_url()
parsed_url = urllibparse.urlparse(url)
parsed_qs = urllibparse.parse_qs(parsed_url.query)
self.assertEqual(parsed_qs['state'][0], state)
def test_get_authorize_url_passes_state_from_func_call(self):
state = "STATE"
oauth = SpotifyOAuth("CLID", "CLISEC", "REDIR", "NOT STATE")
url = oauth.get_authorize_url(state=state)
parsed_url = urllibparse.urlparse(url)
parsed_qs = urllibparse.parse_qs(parsed_url.query)
self.assertEqual(parsed_qs['state'][0], state)
def _load_token(self):
try:
import spotipy.oauth2
callback_url = '{}{}'.format(self.hass.config.api.base_url, AUTH_CALLBACK_PATH)
cache = self.config.get(CONF_CACHE_PATH, self.hass.config.path(DEFAULT_CACHE_PATH))
self.oauth = spotipy.oauth2.SpotifyOAuth(
self.config.get(CONF_CLIENT_ID), self.config.get(CONF_CLIENT_SECRET),
callback_url, scope=SCOPE,
cache_path=cache)
self.token_info = self.oauth.get_cached_token()
except Exception as e:
LOGGER.error("Could not refresh token")
LOGGER.error(e)
def activate_integration(self):
client_id = os.environ.get('SPOTIFY_KEY', '')
client_secret = os.environ.get('SPOTIFY_SECRET', '')
sp_oauth = SpotifyOAuth(client_id, client_secret, None)
token_info = sp_oauth.refresh_access_token(self.integration.refresh_token)
self.integration.access_token = token_info['access_token']
self.integration.refresh_token = token_info['refresh_token']
self.integration.save()
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Spotify platform."""
callback_url = f"{hass.config.api.base_url}{AUTH_CALLBACK_PATH}"
cache = config.get(CONF_CACHE_PATH, hass.config.path(DEFAULT_CACHE_PATH))
oauth = spotipy.oauth2.SpotifyOAuth(
config.get(CONF_CLIENT_ID),
config.get(CONF_CLIENT_SECRET),
callback_url,
scope=SCOPE,
cache_path=cache,
)
token_info = oauth.get_cached_token()
if not token_info:
_LOGGER.info("no token; requesting authorization")
hass.http.register_view(SpotifyAuthCallbackView(config, add_entities, oauth))
request_configuration(hass, config, add_entities, oauth)
return
if hass.data.get(DOMAIN):
configurator = hass.components.configurator
configurator.request_done(hass.data.get(DOMAIN))
del hass.data[DOMAIN]
if not client_id:
print('''
You need to set your Spotify API credentials. You can do this by
setting environment variables like so:
export SPOTIPY_CLIENT_ID='your-spotify-client-id'
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'
export SPOTIPY_REDIRECT_URI='your-app-redirect-url'
Get your credentials at
https://developer.spotify.com/my-applications
''')
raise spotipy.SpotifyException(550, -1, 'no credentials set')
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
scope=scope, cache_path=get_cache_path(username))
# try to get a valid token for this user, from the cache,
# if not in the cache, the create a new (this will send
# the user to a web page where they can authorize this app)
token_info = sp_oauth.get_cached_token()
if not token_info:
redirect_uri_parsed = urlparse(redirect_uri)
run_server(redirect_uri_parsed.hostname,
redirect_uri_parsed.port)
auth_url = sp_oauth.get_authorize_url()
try:
def __init__(self, client_id, client_secret, redirect_uri, refresh_token):
"""Initializes the class with the current playback.
Args:
client_id (str): Client id for your Spotify application
redirect_uri (str): Redirect URI used by your Spotify
application.
refresh_token (str): Refresh token given by Spotify to
update your credentials.
"""
self.auth = oauth2.SpotifyOAuth(client_id,
client_secret,
redirect_uri)
self.refresh_token = refresh_token
self.data = self.current_playback()