mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
Use client credentials flow to fix broken non-authorized requests
This commit is contained in:
parent
4c2c1d763a
commit
63eebfa930
@ -1,3 +1,4 @@
|
|||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import sys
|
import sys
|
||||||
import spotipy
|
import spotipy
|
||||||
|
|
||||||
@ -28,7 +29,8 @@ def show_artist_albums(artist):
|
|||||||
seen.add(name)
|
seen.add(name)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print(('Usage: {0} artist name'.format(sys.argv[0])))
|
print(('Usage: {0} artist name'.format(sys.argv[0])))
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import sys
|
import sys
|
||||||
import spotipy
|
import spotipy
|
||||||
|
|
||||||
@ -47,7 +48,8 @@ def show_artist(artist):
|
|||||||
print('Genres: ', ','.join(artist['genres']))
|
print('Genres: ', ','.join(artist['genres']))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
sp.trace = False
|
sp.trace = False
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
# shows artist info for a URN or URL
|
# shows artist info for a URN or URL
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -9,6 +10,7 @@ if len(sys.argv) > 1:
|
|||||||
else:
|
else:
|
||||||
search_str = 'Radiohead'
|
search_str = 'Radiohead'
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
result = sp.search(search_str)
|
result = sp.search(search_str)
|
||||||
pprint.pprint(result)
|
pprint.pprint(result)
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
# shows album info for a URN or URL
|
# shows album info for a URN or URL
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -11,6 +12,7 @@ else:
|
|||||||
urn = 'spotify:album:5yTx83u3qerZF7GRJu7eFk'
|
urn = 'spotify:album:5yTx83u3qerZF7GRJu7eFk'
|
||||||
|
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
album = sp.album(urn)
|
album = sp.album(urn)
|
||||||
pprint.pprint(album)
|
pprint.pprint(album)
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
# shows artist info for a URN or URL
|
# shows artist info for a URN or URL
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -9,7 +10,8 @@ if len(sys.argv) > 1:
|
|||||||
else:
|
else:
|
||||||
urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu'
|
urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu'
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
artist = sp.artist(urn)
|
artist = sp.artist(urn)
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
# shows artist info for a URN or URL
|
# shows artist info for a URN or URL
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -9,7 +10,8 @@ if len(sys.argv) > 1:
|
|||||||
else:
|
else:
|
||||||
urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu'
|
urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu'
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
response = sp.artist_top_tracks(urn)
|
response = sp.artist_top_tracks(urn)
|
||||||
|
|
||||||
for track in response['tracks']:
|
for track in response['tracks']:
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
# shows related artists for the given seed artist
|
# shows related artists for the given seed artist
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -10,7 +11,8 @@ if len(sys.argv) > 1:
|
|||||||
else:
|
else:
|
||||||
artist_name = 'weezer'
|
artist_name = 'weezer'
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
result = sp.search(q='artist:' + artist_name, type='artist')
|
result = sp.search(q='artist:' + artist_name, type='artist')
|
||||||
try:
|
try:
|
||||||
name = result['artists']['items'][0]['name']
|
name = result['artists']['items'][0]['name']
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
# shows track info for a URN or URL
|
# shows track info for a URN or URL
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -9,6 +10,7 @@ if len(sys.argv) > 1:
|
|||||||
else:
|
else:
|
||||||
urn = 'spotify:track:0Svkvt5I79wficMFgaqEQJ'
|
urn = 'spotify:track:0Svkvt5I79wficMFgaqEQJ'
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
track = sp.track(urn)
|
track = sp.track(urn)
|
||||||
pprint.pprint(track)
|
pprint.pprint(track)
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
given a list of track IDs show the artist and track name
|
given a list of track IDs show the artist and track name
|
||||||
'''
|
'''
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import sys
|
import sys
|
||||||
import spotipy
|
import spotipy
|
||||||
|
|
||||||
@ -14,7 +15,8 @@ if __name__ == '__main__':
|
|||||||
file = sys.stdin
|
file = sys.stdin
|
||||||
tids = file.read().split()
|
tids = file.read().split()
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
for start in range(0, len(tids), max_tracks_per_call):
|
for start in range(0, len(tids), max_tracks_per_call):
|
||||||
results = sp.tracks(tids[start: start + max_tracks_per_call])
|
results = sp.tracks(tids[start: start + max_tracks_per_call])
|
||||||
for track in results['tracks']:
|
for track in results['tracks']:
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
# shows artist info for a URN or URL
|
# shows artist info for a URN or URL
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
import pprint
|
||||||
@ -10,7 +11,8 @@ if len(sys.argv) > 1:
|
|||||||
else:
|
else:
|
||||||
username = 'plamere'
|
username = 'plamere'
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
sp.trace = True
|
sp.trace = True
|
||||||
user = sp.user(username)
|
user = sp.user(username)
|
||||||
pprint.pprint(user)
|
pprint.pprint(user)
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
sp = spotipy.Spotify()
|
|
||||||
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
results = sp.search(q='weezer', limit=20)
|
results = sp.search(q='weezer', limit=20)
|
||||||
for i, t in enumerate(results['tracks']['items']):
|
for i, t in enumerate(results['tracks']['items']):
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
|
|
||||||
|
|
||||||
birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
|
birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
|
||||||
|
|
||||||
spotify = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
results = spotify.artist_albums(birdy_uri, album_type='album')
|
results = spotify.artist_albums(birdy_uri, album_type='album')
|
||||||
albums = results['items']
|
albums = results['items']
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
|
|
||||||
|
|
||||||
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
|
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
|
||||||
|
|
||||||
spotify = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
results = spotify.artist_top_tracks(lz_uri)
|
results = spotify.artist_top_tracks(lz_uri)
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
spotify = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
name = ' '.join(sys.argv[1:])
|
name = ' '.join(sys.argv[1:])
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import random
|
import random
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
@ -5,9 +6,12 @@ import simplejson as json
|
|||||||
'''
|
'''
|
||||||
generates a list of songs where the first word in each subsequent song
|
generates a list of songs where the first word in each subsequent song
|
||||||
matches the last word of the previous song.
|
matches the last word of the previous song.
|
||||||
|
|
||||||
|
usage: python title_chain.py [song name]
|
||||||
'''
|
'''
|
||||||
|
|
||||||
sp = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
|
|
||||||
skiplist = set(['dm', 'remix'])
|
skiplist = set(['dm', 'remix'])
|
||||||
|
|||||||
@ -1,9 +1,13 @@
|
|||||||
# shows tracks for the given artist
|
# shows tracks for the given artist
|
||||||
|
|
||||||
|
# usage: python tracks.py [artist name]
|
||||||
|
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
import spotipy
|
import spotipy
|
||||||
import sys
|
import sys
|
||||||
sp = spotipy.Spotify()
|
|
||||||
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
artist_name = ' '.join(sys.argv[1:])
|
artist_name = ' '.join(sys.argv[1:])
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import unittest
|
|||||||
import pprint
|
import pprint
|
||||||
import requests
|
import requests
|
||||||
from spotipy.client import SpotifyException
|
from spotipy.client import SpotifyException
|
||||||
|
from spotipy.oauth2 import SpotifyClientCredentials
|
||||||
|
|
||||||
|
|
||||||
class TestSpotipy(unittest.TestCase):
|
class TestSpotipy(unittest.TestCase):
|
||||||
@ -23,7 +24,8 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
bad_id = 'BAD_ID'
|
bad_id = 'BAD_ID'
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.spotify = spotipy.Spotify()
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
self.spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
|
||||||
|
|
||||||
def test_artist_urn(self):
|
def test_artist_urn(self):
|
||||||
artist = self.spotify.artist(self.radiohead_urn)
|
artist = self.spotify.artist(self.radiohead_urn)
|
||||||
@ -121,11 +123,12 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
self.assertTrue(found)
|
self.assertTrue(found)
|
||||||
|
|
||||||
def test_search_timeout(self):
|
def test_search_timeout(self):
|
||||||
sp = spotipy.Spotify(requests_timeout=.1)
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_timeout=.1)
|
||||||
try:
|
try:
|
||||||
results = sp.search(q='my*', type='track')
|
results = sp.search(q='my*', type='track')
|
||||||
self.assertTrue(False, 'unexpected search timeout')
|
self.assertTrue(False, 'unexpected search timeout')
|
||||||
except requests.ReadTimeout:
|
except requests.exceptions.Timeout:
|
||||||
self.assertTrue(True, 'expected search timeout')
|
self.assertTrue(True, 'expected search timeout')
|
||||||
|
|
||||||
|
|
||||||
@ -169,12 +172,14 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
from requests import Session
|
from requests import Session
|
||||||
sess = Session()
|
sess = Session()
|
||||||
sess.headers["user-agent"] = "spotipy-test"
|
sess.headers["user-agent"] = "spotipy-test"
|
||||||
with_custom_session = spotipy.Spotify(requests_session=sess)
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
with_custom_session = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_session=sess)
|
||||||
self.assertTrue(with_custom_session.user(user="akx")["uri"] == "spotify:user:akx")
|
self.assertTrue(with_custom_session.user(user="akx")["uri"] == "spotify:user:akx")
|
||||||
|
|
||||||
def test_force_no_requests_session(self):
|
def test_force_no_requests_session(self):
|
||||||
from requests import Session
|
from requests import Session
|
||||||
with_no_session = spotipy.Spotify(requests_session=False)
|
client_credentials_manager = SpotifyClientCredentials()
|
||||||
|
with_no_session = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_session=False)
|
||||||
self.assertFalse(isinstance(with_no_session._session, Session))
|
self.assertFalse(isinstance(with_no_session._session, Session))
|
||||||
self.assertTrue(with_no_session.user(user="akx")["uri"] == "spotify:user:akx")
|
self.assertTrue(with_no_session.user(user="akx")["uri"] == "spotify:user:akx")
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user