I just started working with Spotify's Web API and am using Python/Spotipy to retrieve data. If I want to retrieve data related specifically to my personal account (like recently played tracks or saved albums), do I need to set a redirect URI in my application settings? If so, what's a quick and easy URI that I could use?
I'm not building a public facing application as of now - I just want to retrieve historical data about my own Spotify usage. So I'm following the examples posted on Spotipy's GitHub (like this one: https://github.com/plamere/spotipy/blob/master/examples/user_playlists.py) but I'm getting the following error:
"SpotifyOauthError: No redirect_uri. Pass it or set a SPOTIPY_REDIRECT_URI environment variable."
If you are using spotipy, the redirect URI doesn't matter, just use http://localhost/
Spotipy will display a URL, open that in a browser and copy and paste back the response code.
Hi murraypaul and thanks for replying! I tried your suggestion to use "http://localhost/" as the redirect URI (and I double checked to make sure that my app settings have the same URI) but I get an error:
I'm just going off of the example under "Quick start" in the docs and running the following code in a Jupyter Notebook:
import spotipy from spotipy.oauth2 import SpotifyOAuth scope = "user-library-read" sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope)) results = sp.current_user_saved_tracks() for idx, item in enumerate(results['items']): track = item['track'] print(idx, track['artists']['name'], " – ", track['name'])
I've tried other things, like using "http://localhost:8888/callback/" as the URI but nothing seems to work. I keep getting errors.
You will get an error, but you just need to capture the URL you are redirected to, and give that to Spotipy when it asks for it.
What should happen is:
a) Spotipy opens a browser and connects to the authorisation service.
b) The authorisation service redirects your browser to http://localhost?code=...
c) Spotipy shows a prompt: "Enter the URL you were redirected to:"
d) You copy the entire URL from your browser address bar and paste it into the console window python is running in.
It is quite clunky, but you only need to do it the first time you request a new type of access, spotipy will cache the responses.
Alternatively, if you have a publicly addressable IP, you can specify your redirect URL as http://hostname.domain:port/, and spotipy will spin up a temporary web server on that port to handle the redirect. I have not used that myself, however.
Awesome it worked! Thanks so much.
On a related note, I tried following Spotify's Quick Start directions for setting up a server-side application with Node.js but I'm getting an error at the "Running the Application" step. I can get to step 3 of that section but when I click on the login button the browser goes to a page that says "INVALID_CLIENT: Invalid redirect URI." In the docs it looks like that step should take me to a login page where I can input my Spotify username and password.
Any ideas what I'm doing wrong?
Make sure the URL you added as redirect_uri is included in your Redirect URIs.
1 - Go to your dashboard
2 - Click on edit settings
3 - Add the URL under Redirect URIs, press Add
4 - Save
For posterity, I was stuck on this issue until I tried "http://localhost" instead of "http://localhost/". The latter was taking me to a page that said it was an invalid URL, while the former took me to a page where I could click on a button allowing the app to have access to stuff, idk what it said but you get the gist. When using the former, it lined up with the other comments (click yes, copy paste the new url into python, and smile cause it works)
very helpful, thank you!
here's how I did it for username:
from spotipy.oauth2 import SpotifyClientCredentials
cid = <YOUR CLIENT ID>
secret = <YOUR CLIENT SECRET>
ccm = SpotifyClientCredentials(client_id = cid, client_secret = secret)
sp = spotipy.Spotify(client_credentials_manager = ccm)
os.environ["SPOTIPY_CLIENT_ID"] = cid
os.environ["SPOTIPY_CLIENT_SECRET"] = secret
os.environ["SPOTIPY_REDIRECT_URI"] = "https://localhost:8080"
scope = 'user-library-read'
username = <YOUR USERNAME HERE>
token = spotipy.util.prompt_for_user_token(username, scope)
spotipy_obj = spotipy.Spotify(auth=token)
saved_tracks_resp = spotipy_obj.current_user_saved_tracks(limit=50)
print('Couldn\'t get token for that username')
To use Spotipy in Google Colab, you can follow the steps in this Stack Overflow answer: https://stackoverflow.com/a/68651136/7111585
Hey there you, Yeah, you! 😁 Welcome - we're glad you joined the Spotify Community! While you here, let's have a fun game…