[GH-ISSUE #467] Getting auth token fails in localhost:8080 #272

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

Originally created by @dpeite on GitHub (Apr 11, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/467

Hello everyone,

I just download the last release and executing the example simple4.py fails with the next message error.

Traceback (most recent call last):
  File "/snap/pycharm-professional/171/helpers/pydev/pydevd.py", line 1415, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-professional/171/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/peite/PycharmProjects/random_spotify_playlist/test.py", line 24, in <module>
    main()
  File "/home/peite/PycharmProjects/random_spotify_playlist/test.py", line 19, in main
    me = spotify.me()
  File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 825, in me
    return self._get("me/")
  File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 205, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 149, in _internal_call
    headers = self._auth_headers()
  File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 140, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/oauth2.py", line 426, in get_access_token
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

Digging throught the project I see that the error comes from the function parse_response_code().

This function is called from get_authorization_code and always receives a string:

    def get_authorization_code(self, response=None):
        return self.parse_response_code(response or self.get_auth_response())

Always receive a string because function get_auth_response() calls the methods _get_auth_response_local_server or _get_auth_response_interactive depending of the redirect uri, but these functions always return directly the auth code string.

    def get_auth_response(self):
        logger.info('User authentication requires interaction with your '
                    'web browser. Once you enter your credentials and '
                    'give authorization, you will be redirected to '
                    'a url.  Paste that url you were directed to to '
                    'complete the authorization.')

        redirect_info = urlparse(self.redirect_uri)
        redirect_host, redirect_port = get_host_port(redirect_info.netloc)

        if redirect_host in ("127.0.0.1", "localhost") and redirect_info.scheme == "http":
            return self._get_auth_response_local_server(redirect_port)
        else:
            logger.info('Paste that url you were directed to in order to '
                        'complete the authorization')
            return self._get_auth_response_interactive()
    def _get_auth_response_local_server(self, redirect_port):
        server = start_local_http_server(redirect_port)
        self._open_auth_url()
        server.handle_request()

        if server.auth_code is not None:
            return server.auth_code
        elif server.error is not None:
            raise SpotifyOauthError("Received error from OAuth server: {}".format(server.error))
        else:
            raise SpotifyOauthError("Server listening on localhost has not been accessed")
    def _get_auth_response_interactive(self):
        self._open_auth_url()
        try:
            response = raw_input("Enter the URL you were redirected to: ")
        except NameError:
            response = input("Enter the URL you were redirected to: ")

        return self.parse_response_code(response)

I think that it's not necessary the call to parse_response_code in function get_authorization_code.

    def get_authorization_code(self, response=None):
        return response or self.get_auth_response()
Originally created by @dpeite on GitHub (Apr 11, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/467 Hello everyone, I just download the last release and executing the example `simple4.py` fails with the next message error. ```bash Traceback (most recent call last): File "/snap/pycharm-professional/171/helpers/pydev/pydevd.py", line 1415, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/snap/pycharm-professional/171/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/peite/PycharmProjects/random_spotify_playlist/test.py", line 24, in <module> main() File "/home/peite/PycharmProjects/random_spotify_playlist/test.py", line 19, in main me = spotify.me() File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 825, in me return self._get("me/") File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 205, in _get return self._internal_call("GET", url, payload, kwargs) File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 149, in _internal_call headers = self._auth_headers() File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/client.py", line 140, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) File "/home/peite/PycharmProjects/random_spotify_playlist/venv/lib/python3.6/site-packages/spotipy/oauth2.py", line 426, in get_access_token raise SpotifyOauthError(response.reason) spotipy.oauth2.SpotifyOauthError: Bad Request ``` Digging throught the project I see that the error comes from the function `parse_response_code()`. This function is called from `get_authorization_code` and always receives a string: ```python def get_authorization_code(self, response=None): return self.parse_response_code(response or self.get_auth_response()) ``` Always receive a string because function `get_auth_response()` calls the methods `_get_auth_response_local_server` or `_get_auth_response_interactive` depending of the redirect uri, but these functions always return directly the auth code string. ```python def get_auth_response(self): logger.info('User authentication requires interaction with your ' 'web browser. Once you enter your credentials and ' 'give authorization, you will be redirected to ' 'a url. Paste that url you were directed to to ' 'complete the authorization.') redirect_info = urlparse(self.redirect_uri) redirect_host, redirect_port = get_host_port(redirect_info.netloc) if redirect_host in ("127.0.0.1", "localhost") and redirect_info.scheme == "http": return self._get_auth_response_local_server(redirect_port) else: logger.info('Paste that url you were directed to in order to ' 'complete the authorization') return self._get_auth_response_interactive() ``` ```python def _get_auth_response_local_server(self, redirect_port): server = start_local_http_server(redirect_port) self._open_auth_url() server.handle_request() if server.auth_code is not None: return server.auth_code elif server.error is not None: raise SpotifyOauthError("Received error from OAuth server: {}".format(server.error)) else: raise SpotifyOauthError("Server listening on localhost has not been accessed") ``` ```python def _get_auth_response_interactive(self): self._open_auth_url() try: response = raw_input("Enter the URL you were redirected to: ") except NameError: response = input("Enter the URL you were redirected to: ") return self.parse_response_code(response) ``` I think that it's not necessary the call to `parse_response_code` in function `get_authorization_code`. ```python def get_authorization_code(self, response=None): return response or self.get_auth_response() ```
kerem closed this issue 2026-02-27 23:21:44 +03:00
Author
Owner

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

Hello @dpeite,

Nice catch! Thanks for the pointers. I'm trying to fix this now and will keep you updated

<!-- gh-comment-id:612452653 --> @stephanebruckert commented on GitHub (Apr 11, 2020): Hello @dpeite, Nice catch! Thanks for the pointers. I'm trying to fix this now and will keep you updated
Author
Owner

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

This as well as other URL-parsing related issues should now be fixed in 2.11.1. Can you please confirm if you get a chance?

Thanks a lot for the report!

<!-- gh-comment-id:612462789 --> @stephanebruckert commented on GitHub (Apr 11, 2020): This as well as other URL-parsing related issues should now be fixed in 2.11.1. Can you please confirm if you get a chance? Thanks a lot for the report!
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#272
No description provided.