Using cached credentials.json for authentication in python-spotipy #928
Replies: 4 comments 4 replies
-
Well done upping your Rust game. We can use the help 😉 How are you launching |
Beta Was this translation helpful? Give feedback.
-
python-spotipy uses Spotify's Web API, right? Which requires a valid Web API access token to login with. The auth_data in your librespot cached credentials file is not a Web API access token, it's for another login method which python-spotipy has no idea about. librespot can use the auth_data to login and from there it can get a Web API access token. So no, you cannot use the cached librespot credentials directly with python-spotipy, you need something like your get_token program to bridge the two systems. EDit: replied to the right message this time |
Beta Was this translation helpful? Give feedback.
-
You could also look at |
Beta Was this translation helpful? Give feedback.
-
Took me a while to get back to put everything together, but this solution has been running for a while now. Works nicely. My modified get_token can be found here. Pass it the path to the directory containing I've also made a version get_token_args.rs, for convenience, which accepts auth_data and username as argument on the command line. I had a bit of a challenge cross compiling it. Because my newer Ubuntu 21.10 had a newer glibc version than the RPI. So I created this Dockerfile to create a container for cross compiling. And finally, I made a small Python wrapper around I'm using it for example like this: #!/usr/bin/env python3
import spotipy
from librespot_token import token
spotify = spotipy.Spotify(auth=token())
state = spotify.current_playback()
if not state or not state['is_playing']:
spotify.start_playback()
else:
spotify.pause_playback() Hopefully this is useful for someone. |
Beta Was this translation helpful? Give feedback.
-
Hey there,
Thank you for the excellent work with librespot. Really enjoying it.
I've been trying to build a method to control the playback of librespot on the same headless RPI as where it's running. Mainly to be able to operate it using a infrared remote (through lirc) and GPIO buttons. I've read from other threads the intend is to not build API calls in librespot directly. So the Spotify API seemed like the best alternative. Because my Rust is ahem... rusty, and my Python is better I was considering to use python-spotipy.
I'm connecting to librespot in discovery mode. So I don't have a username/password, but
auth_type=1
andauth_data=[string]
in the cachedcredentials.json
.I've been unable to find a method to use those cached credential in the python-spotipy library. It's either missing from there, or I'm not understanding it. Trying to find a workaround, I've hacked my way in the
get_token
example of librespot to use the cached credentials.In
librespot/examples/get_token.rs
:Call with:
And this works! If I pass a directory with an existing credentials.json (even on my laptop) and the client ID of my Spotify App, I will get back a valid token which I can use in the python-spotipy code again.
So ahem.. this works. And I guess I should/could just build my control calls directly in Rust, based on this example, and not use python-spotipy. But out of interest, what is going on here? Is there a way to use the cached librespot credentials.json directly for authentication in python-spotipy?
Thanks,
Paul
Beta Was this translation helpful? Give feedback.
All reactions