Skip to content

Instantly share code, notes, and snippets.

@lasconic
Last active November 29, 2016 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lasconic/b8e2543ecf81bbf36c0a313cec2f6674 to your computer and use it in GitHub Desktop.
Save lasconic/b8e2543ecf81bbf36c0a313cec2f6674 to your computer and use it in GitHub Desktop.
Get a cache file with authorization settings for spotify. Useful for the spotify kodi addon http://forum.kodi.tv/showthread.php?tid=265356&page=33
# $ virtualenv venv
# $ source venv/bin/activate
# $ pip install spotipy
# $ python spotify_auth.py YOURUSERNAME
# Follow the instruction, no problem with the 404
# It will create a file .cache-YOURUSERNAME, copy it to .kodi/userdata/addon_data/plugin.audio.spotify as YOURUSERNAME.cache
# Run the spotify addon. If you have an error, make sure your Kodi time is right.
import sys
import spotipy
import spotipy.util as util
scope = "playlist-read-private playlist-read-collaborative playlist-modify-public playlist-modify-private user-follow-modify user-follow-read user-library-read user-library-modify user-read-private user-read-email user-read-birthdate user-top-read"
client_id = '4940f5cc79b149af9f71d5ef9319eff0'
client_secret = '779f4d60bd3b42e29984adf423f19688'
port = 52308
redirect_uri = 'http://localhost:%s/callback' % port
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print "Usage: %s username" % (sys.argv[0],)
sys.exit()
token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print track['name'] + ' - ' + track['artists'][0]['name']
else:
print "Can't get token for", username
@FrankTeunisse
Copy link

Thanks for the script! Unfortunately it's not giving me a valid token. I get the response as stated below. This is the same exception as I find in the Kodi log file after providing my credentials in the latest version of the Kodi Spotify plugin (1.0.25).

Traceback (most recent call last):
  File "spotify_auth.py", line 25, in <module>
    token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
  File "/usr/local/lib/python2.7/dist-packages/spotipy/util.py", line 86, in prompt_for_user_token
    token_info = sp_oauth.get_access_token(code)
  File "/usr/local/lib/python2.7/dist-packages/spotipy/oauth2.py", line 210, in get_access_token
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment