Redirect URI needed?

Reply

Redirect URI needed?

burnssh
Casual Listener

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."

4 Replies

Re: Redirect URI needed?

murraypaul
Music Fan

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.

Re: Redirect URI needed?

burnssh
Casual Listener

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:

 

localhost-error.png

 

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'][0]['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.

Re: Redirect URI needed?

murraypaul
Music Fan

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.

Re: Redirect URI needed?

burnssh
Casual Listener

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?

SUGGESTED POSTS