本文整理汇总了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')