本文整理匯總了Python中spotipy.oauth2.SpotifyClientCredentials方法的典型用法代碼示例。如果您正苦於以下問題:Python oauth2.SpotifyClientCredentials方法的具體用法?Python oauth2.SpotifyClientCredentials怎麽用?Python oauth2.SpotifyClientCredentials使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類spotipy.oauth2
的用法示例。
在下文中一共展示了oauth2.SpotifyClientCredentials方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_metadata
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def get_metadata(file_name, client_id, client_secret):
"""
Tries finding metadata through Spotify
"""
song_name = improve_name(file_name) # Remove useless words from title
client_credentials_manager = SpotifyClientCredentials(client_id, client_secret)
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
results = spotify.search(song_name, limit=1)
results = results['tracks']['items'][0] # Find top result
album = results['album']['name'] # Parse json dictionary
artist = results['album']['artists'][0]['name']
song_title = results['name']
album_art = results['album']['images'][0]['url']
return artist, album, song_title, album_art
示例2: __init__
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def __init__(self):
self.client_credentials_manager = SpotifyClientCredentials()
self.spotify = spotipy.Spotify(client_credentials_manager=self.client_credentials_manager)
示例3: getMainArtistGenre
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def getMainArtistGenre(self):
uri = self.trackdata["artists"][0]["uri"]
client_credentials_manager = SpotifyClientCredentials()
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
artistdata = spotify.artist(uri)
try:
return artistdata["genres"][0]
except IndexError:
return None
示例4: _generate_token
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def _generate_token(self, client_id, client_secret):
credentials = oauth2.SpotifyClientCredentials(
client_secret=client_secret,
)
token = credentials.get_access_token()
return token
示例5: __init__
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def __init__(self, client_id=None, client_secret=None):
global masterclient
# `spotipy.Spotify` makes use of `self._session` and would
# result in an error. The below line is a workaround.
self._session = None
credentials_provided = client_id is not None \
and client_secret is not None
valid_input = credentials_provided or masterclient is not None
if not valid_input:
raise SpotifyAuthorizationError(
"You must pass in client_id and client_secret to this method "
"when authenticating for the first time."
)
if masterclient:
logger.debug("Reading cached master Spotify credentials.")
# Use cached client instead of authorizing again
# and thus wasting time.
self.__dict__.update(masterclient.__dict__)
else:
logger.debug("Setting master Spotify credentials.")
credential_manager = oauth2.SpotifyClientCredentials(
client_id=client_id,
client_secret=client_secret
)
super().__init__(client_credentials_manager=credential_manager)
# Cache current client
masterclient = self
示例6: getCredentials
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def getCredentials(self):
try:
return spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
except spotipy.oauth2.SpotifyOauthError:
print('Did not find Spotify credentials.')
print('Please visit https://github.com/bjarneo/pytify#credentials for more information.')
sys.exit(1)
# query
示例7: _get_spotify
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def _get_spotify():
spotify = engine.getEnv('spotify')
if spotify == None:
auth_token = engine.getEnv('spotify_auth_token')
if auth_token:
spotify = spotipy.Spotify(auth=auth_token)
else:
spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
spotify.trace_out = True
engine.setEnv('spotify', spotify)
return spotify
示例8: test_spotify_client_credentials_get_access_token
# 需要導入模塊: from spotipy import oauth2 [as 別名]
# 或者: from spotipy.oauth2 import SpotifyClientCredentials [as 別名]
def test_spotify_client_credentials_get_access_token(self):
oauth = SpotifyClientCredentials(client_id='ID', client_secret='SECRET')
with self.assertRaises(SpotifyOauthError) as error:
oauth.get_access_token()
self.assertEqual(error.exception.error, 'invalid_client')