[GH-ISSUE #789] Use Spotipy OAuth authorization from inside a Docker container #487

Open
opened 2026-02-27 23:22:54 +03:00 by kerem · 3 comments
Owner

Originally created by @jkulak on GitHub (Mar 8, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/789

I am trying to authenticate, to get my followed artists.

My authentication code. I also tried with SpotifyOAuth:

sp = spotipy.Spotify(
    auth_manager=SpotifyPKCE(scope=SPOTIFY_SCOPE, open_browser=True)
)

My code is being run inside a Docker FROM python:3.10.2-alpine3.15 container.

Browser is not being opened (I guess it's ok, it's trying to open it on a container level not on my OS/host level), but when I open the given URL in the browser i.e.:

https://accounts.spotify.com/authorize?client_id=4ec8[...]1bc0b&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A3002&code_challenge_method=S256&code_challenge=y80Z9Y7o5n4nq6BgnKr2b0WBrT_KpVQyw9AjDl051jA&scope=user-follow-read

It seems like the http server from Spotipy is not running and not waiting for the /?code=... parameter. I think all the proper ports are exposed from Docker correctly and forwarded when creating containers -p 3002:3002. Spotify Application has it's Redirect URI configured properly (with the port etc.).

Still, I'm getting

This page isn’t working
127.0.0.1 didn’t send any data.
ERR_EMPTY_RESPONSE

in the browser, and Spotipy is waiting for the code.

Using open_browser=False and manually pasting the url with code works - but that's not what I'm looking for.

Originally created by @jkulak on GitHub (Mar 8, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/789 I am trying to authenticate, to get my followed artists. My authentication code. I also tried with `SpotifyOAuth`: ``` sp = spotipy.Spotify( auth_manager=SpotifyPKCE(scope=SPOTIFY_SCOPE, open_browser=True) ) ``` My **code is being run inside a Docker** `FROM python:3.10.2-alpine3.15` container. Browser is not being opened (I guess it's ok, it's trying to open it on a container level not on my OS/host level), ***but*** when I open the given URL in the browser i.e.: `https://accounts.spotify.com/authorize?client_id=4ec8[...]1bc0b&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A3002&code_challenge_method=S256&code_challenge=y80Z9Y7o5n4nq6BgnKr2b0WBrT_KpVQyw9AjDl051jA&scope=user-follow-read` It seems like the http server from Spotipy is not running and not waiting for the `/?code=...` parameter. I think all the proper ports are exposed from Docker correctly and forwarded when creating containers `-p 3002:3002`. Spotify Application has it's Redirect URI configured properly (with the port etc.). Still, I'm getting ``` This page isn’t working 127.0.0.1 didn’t send any data. ERR_EMPTY_RESPONSE ``` in the browser, and Spotipy is waiting for the code. Using `open_browser=False` and manually pasting the url with code works - but that's not what I'm looking for.
Author
Owner

@jkulak commented on GitHub (Mar 9, 2022):

I solved it by changing below line:
github.com/plamere/spotipy@9a627e88f4/spotipy/oauth2.py (L1303)

to:

server = HTTPServer(("0.0.0.0", port), handler) (meaning, Spotipy is listening on any IP address).

I would suggest a solution either to read that value from ENV ie. SPOTIPY_SERVER_IP which could default to 127.0.0.1 no to break current functionality.

127.0.0.0.1 will not work inside a Docker container.

<!-- gh-comment-id:1063167312 --> @jkulak commented on GitHub (Mar 9, 2022): I solved it by changing below line: https://github.com/plamere/spotipy/blob/9a627e88f422927822ce39ae9919cc7ab9813dde/spotipy/oauth2.py#L1303 to: ```server = HTTPServer(("0.0.0.0", port), handler)``` (meaning, Spotipy is listening on ***any*** IP address). I would suggest a solution either to read that value from ENV ie. `SPOTIPY_SERVER_IP` which could default to `127.0.0.1` no to break current functionality. `127.0.0.0.1` will not work inside a Docker container.
Author
Owner

@Dielee commented on GitHub (Jul 7, 2022):

@jkulak how do you set the port for Spotipy http Server ?

<!-- gh-comment-id:1178016813 --> @Dielee commented on GitHub (Jul 7, 2022): @jkulak how do you set the port for Spotipy http Server ?
Author
Owner

@jkulak commented on GitHub (Aug 4, 2022):

Hi @Dielee. It's being read from SPOTIPY_REDIRECT_URI env variable.

You can see it here in the code: github.com/plamere/spotipy@9a627e88f4/spotipy/oauth2.py (L478)

Port is a part of URI, so I just set it to SPOTIPY_REDIRECT_URI=http://127.0.0.1:8083.

<!-- gh-comment-id:1205862468 --> @jkulak commented on GitHub (Aug 4, 2022): Hi @Dielee. It's being read from `SPOTIPY_REDIRECT_URI` env variable. You can see it here in the code: https://github.com/plamere/spotipy/blob/9a627e88f422927822ce39ae9919cc7ab9813dde/spotipy/oauth2.py#L478 Port is a part of URI, so I just set it to `SPOTIPY_REDIRECT_URI=http://127.0.0.1:8083`.
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#487
No description provided.