[GH-ISSUE #471] Following Getting Started docs, can't authenticate with "Authorization Code Flow" #275

Closed
opened 2026-02-27 23:21:45 +03:00 by kerem · 5 comments
Owner

Originally created by @mfeif on GitHub (Apr 14, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/471

Hi There.

I'm following verbatim the docs here. I have all the env variables set, and a premium account and all of that. I'm using localhost as per the instructions.

The browser can't connect to the redirected URL (which is http://localhost/?code=AQCBPehFt16_roFv...{long hash}) I've pasted the URL from the browser into the console as per the docs, and press return, and nothing happens. No error messages, no feedback, nothing.

Looking around the code, it seems like it is supposed to launch an httpd there, but I can't see it.

If I ctrl-C the script, this traceback results:

Traceback (most recent call last):
  File "example.py", line 13, in <module>
    token = util.prompt_for_user_token(username, scope)
  File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/util.py", line 91, in prompt_for_user_token
    code = sp_oauth.get_auth_response()
  File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 366, in get_auth_response
    return self._get_auth_response_local_server(redirect_port)
  File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 346, in _get_auth_response_local_server
    server.handle_request()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 294, in handle_request
    ready = selector.select(timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/selectors.py", line 415, in select
    fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt

I tried using an unprivileged port (http://localhost:8001/) and I get an "INVALID_CLIENT: Invalid redirect URI" message from Spotify.

It doesn't seem like the script is running anything on localhost for the browser to connect to.

I'm on python 3.7.2 and spotipy is 2.11.1, running on macos.

What am I missing? Thanks!

Originally created by @mfeif on GitHub (Apr 14, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/471 Hi There. I'm following verbatim the docs [here](https://spotipy.readthedocs.io/en/latest/#authorization-code-flow). I have all the env variables set, and a premium account and all of that. I'm using localhost as per the instructions. The browser can't connect to the redirected URL (which is http://localhost/?code=AQCBPehFt16_roFv...{long hash}) I've pasted the URL from the browser into the console as per the docs, and press return, and nothing happens. No error messages, no feedback, nothing. Looking around the code, it seems like it is supposed to launch an httpd there, but I can't see it. If I ctrl-C the script, this traceback results: ```python Traceback (most recent call last): File "example.py", line 13, in <module> token = util.prompt_for_user_token(username, scope) File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/util.py", line 91, in prompt_for_user_token code = sp_oauth.get_auth_response() File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 366, in get_auth_response return self._get_auth_response_local_server(redirect_port) File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 346, in _get_auth_response_local_server server.handle_request() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 294, in handle_request ready = selector.select(timeout) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/selectors.py", line 415, in select fd_event_list = self._selector.poll(timeout) KeyboardInterrupt ``` I tried using an unprivileged port (http://localhost:8001/) and I get an "INVALID_CLIENT: Invalid redirect URI" message from Spotify. It doesn't seem like the script is running anything on localhost for the browser to connect to. I'm on python 3.7.2 and spotipy is 2.11.1, running on macos. What am I missing? Thanks!
kerem 2026-02-27 23:21:45 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Apr 14, 2020):

Hi @mfeif, thanks for the report.

You were on the right track as it looks like it works only when a port is specified.
Try to to use 8001 again, but don't forget to also update the redirect URI in https://developer.spotify.com/dashboard/ for it to be http://localhost:8001

Let me know if that works and I will update the doc (or even if you have time feel free to open a PR to update it)

<!-- gh-comment-id:613499646 --> @stephanebruckert commented on GitHub (Apr 14, 2020): Hi @mfeif, thanks for the report. You were on the right track as it looks like it works only when a port is specified. Try to to use 8001 again, but don't forget to also update the redirect URI in https://developer.spotify.com/dashboard/ for it to be `http://localhost:8001` Let me know if that works and I will update the doc (or even if you have time feel free to open a PR to update it)
Author
Owner

@mfeif commented on GitHub (Apr 14, 2020):

I HAD forgotten to update the url. Thanks. But now I do get back a page that says "Authentication status: successful This window can be closed." But then a crash:

Traceback (most recent call last):
  File "get_token.py", line 9, in <module>
    token = util.prompt_for_user_token(username, scope)
  File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/util.py", line 92, in prompt_for_user_token
    token = sp_oauth.get_access_token(code, as_dict=False)
  File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 425, in get_access_token
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

I can try and run this in a debugger to see what's causing the raise.

<!-- gh-comment-id:613530784 --> @mfeif commented on GitHub (Apr 14, 2020): I HAD forgotten to update the url. Thanks. But now I do get back a page that says "Authentication status: successful This window can be closed." But then a crash: ``` Traceback (most recent call last): File "get_token.py", line 9, in <module> token = util.prompt_for_user_token(username, scope) File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/util.py", line 92, in prompt_for_user_token token = sp_oauth.get_access_token(code, as_dict=False) File "/Users/mjf/Library/Caches/pypoetry/virtualenvs/pifi-GA63gEPW-py3.7/lib/python3.7/site-packages/spotipy/oauth2.py", line 425, in get_access_token raise SpotifyOauthError(response.reason) spotipy.oauth2.SpotifyOauthError: Bad Request ``` I can try and run this in a debugger to see what's causing the raise.
Author
Owner

@stephanebruckert commented on GitHub (Apr 14, 2020):

Can you please share a minimal working example and the environment variable names that you've set? you're still on 2.11.1?

<!-- gh-comment-id:613544013 --> @stephanebruckert commented on GitHub (Apr 14, 2020): Can you please share a minimal working example and the environment variable names that you've set? you're still on 2.11.1?
Author
Owner

@mfeif commented on GitHub (Apr 14, 2020):

Now it's working :-(

The only thing I did was move the credential stuff into the code rather than ENV because my debugger was of course launching its own process and couldn't see my existing env.

If I find any more examples of where it's not working I'll come back here.

So I guess we can close this, but it doesn't seem like the docs are leading people to a successful "Getting Started". I'm not sure if my theory about privileged ports is correct, but it seems likely.

<!-- gh-comment-id:613555804 --> @mfeif commented on GitHub (Apr 14, 2020): Now it's working :-( The only thing I did was move the credential stuff into the code rather than ENV because my debugger was of course launching its own process and couldn't see my existing env. If I find any more examples of where it's not working I'll come back here. So I guess we can close this, but it doesn't seem like the docs are leading people to a successful "Getting Started". I'm not sure if my theory about privileged ports is correct, but it seems likely.
Author
Owner

@jonascj commented on GitHub (Apr 18, 2020):

I've created PR #477 which deals with this issue (it updates the documentation but also changes the authorization process such that it does not hang if one does not specifies a port, i.e. old scripts/applications do not break).

<!-- gh-comment-id:615969174 --> @jonascj commented on GitHub (Apr 18, 2020): I've created PR #477 which deals with this issue (it updates the documentation but also changes the authorization process such that it does not hang if one does not specifies a port, i.e. old scripts/applications do not break).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#275
No description provided.