[GH-ISSUE #492] Authentication hangs when ran in headless mode #294

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

Originally created by @varna9000 on GitHub (May 17, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/492

Hi, I've got the latest release 2.12.0. My simple code is the following:

import spotipy
from spotipy import SpotifyOAuth

client_id = 'xxxxxxxxx'
client_secret = 'xxxxxxxxx'
redirect_uri = 'http://127.0.0.1:8081/'
scope = 'user-read-currently-playing'
username='xxxxxxx'

creds = SpotifyOAuth(username=username, scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
sp = spotipy.Spotify(client_credentials_manager=creds)

print(dir(sp))

try:
    current_song = sp.currently_playing()
    print(current_song)
except:
    raise

It seems the line current_song = sp.currently_playing() hangs the code as it doesn't even raise an exception. I've tried calling other methods but it's the same result. Am I doing something wrong?

Originally created by @varna9000 on GitHub (May 17, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/492 Hi, I've got the latest release 2.12.0. My simple code is the following: ``` import spotipy from spotipy import SpotifyOAuth client_id = 'xxxxxxxxx' client_secret = 'xxxxxxxxx' redirect_uri = 'http://127.0.0.1:8081/' scope = 'user-read-currently-playing' username='xxxxxxx' creds = SpotifyOAuth(username=username, scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri) sp = spotipy.Spotify(client_credentials_manager=creds) print(dir(sp)) try: current_song = sp.currently_playing() print(current_song) except: raise ``` It seems the line `current_song = sp.currently_playing()` hangs the code as it doesn't even raise an exception. I've tried calling other methods but it's the same result. Am I doing something wrong?
kerem 2026-02-27 23:21:50 +03:00
Author
Owner

@stephanebruckert commented on GitHub (May 17, 2020):

Since you need to sign in with your own spotify account via the browser, spotipy should have opened a browser window. So, bunch of questions:

  • did a browser window open?
  • did it automatically close (success)?
  • or did it fail and there is an error message on that web page?

spotipy currently hangs if the authentication response is not successful, which is something we might want to fix! But first let's try to make it for you

<!-- gh-comment-id:629790323 --> @stephanebruckert commented on GitHub (May 17, 2020): Since you need to sign in with your own spotify account via the browser, spotipy should have opened a browser window. So, bunch of questions: - did a browser window open? - did it automatically close (success)? - or did it fail and there is an error message on that web page? spotipy currently hangs if the authentication response is not successful, which is something we might want to fix! But first let's try to make it for you
Author
Owner

@varna9000 commented on GitHub (May 17, 2020):

Trouble is, I'm using headless Raspbian Lite install, so there's no browser and GUI. I thought that if all credentials match, including the redirect URI, all would be done automatically. Isn't that the case?

EDIT: Alternatively, if we can't avoid the browser, perhaps we can forward the url request to a console browser such as "links"?

<!-- gh-comment-id:629790607 --> @varna9000 commented on GitHub (May 17, 2020): Trouble is, I'm using headless Raspbian Lite install, so there's no browser and GUI. I thought that if all credentials match, including the redirect URI, all would be done automatically. Isn't that the case? EDIT: Alternatively, if we can't avoid the browser, perhaps we can forward the url request to a console browser such as "links"?
Author
Owner

@varna9000 commented on GitHub (May 17, 2020):

Ok, I installed Links and now it's automatically opened. The page is not showing correctly only in console, so I copied the url to my GUI browser and I saw the Spotify permission dialog. When I clicked "Agree" it redirects me to 127.0.0.1 (i.e. my Redirect URI), but naturally there is nothing there, since I opened it in another computer (with GUI). I thought that if you set localhost and 127.0.0.1 this Agree process is handled automatically. This was my impression from the docs, but apparently this is not the case.

<!-- gh-comment-id:629795719 --> @varna9000 commented on GitHub (May 17, 2020): Ok, I installed Links and now it's automatically opened. The page is not showing correctly only in console, so I copied the url to my GUI browser and I saw the Spotify permission dialog. When I clicked "Agree" it redirects me to 127.0.0.1 (i.e. my Redirect URI), but naturally there is nothing there, since I opened it in another computer (with GUI). I thought that if you set localhost and 127.0.0.1 this Agree process is handled automatically. This was my impression from the docs, but apparently this is not the case.
Author
Owner

@varna9000 commented on GitHub (May 17, 2020):

Phew, managed to do it, but it shouldn't be that hard. I set 127.0.0.1 as call-back URL (no port). "Links" browser automatically opened, I copied the URL (from processes list "ps ax") and pasted in GUI browser on another computer, clicked "Agree", then copied the full 127.0.0.1 redirect url, quit "Links" and pasted the url in the dialog prompt ("Enter the URL you were redirected to:") . Done. Quite a workaround.

<!-- gh-comment-id:629808318 --> @varna9000 commented on GitHub (May 17, 2020): Phew, managed to do it, but it shouldn't be that hard. I set 127.0.0.1 as call-back URL (no port). "Links" browser automatically opened, I copied the URL (from processes list "ps ax") and pasted in GUI browser on another computer, clicked "Agree", then copied the full 127.0.0.1 redirect url, quit "Links" and pasted the url in the dialog prompt ("Enter the URL you were redirected to:") . Done. Quite a workaround.
Author
Owner

@stephanebruckert commented on GitHub (May 17, 2020):

  • another possibility is to run the script on a different computer and copy the cache file to the raspbian
  • in the future to avoid copying the file from one machine to the other, the token could be stored in the cloud https://github.com/plamere/spotipy/issues/51
  • from your experience, what's currently missing to better help future users? do you think we need a headless mode that prints the URL? in case you would like to open a PR that'd be very welcome! 🙏
<!-- gh-comment-id:629843194 --> @stephanebruckert commented on GitHub (May 17, 2020): - another possibility is to run the script on a different computer and copy the cache file to the raspbian - in the future to avoid copying the file from one machine to the other, the token could be stored in the cloud https://github.com/plamere/spotipy/issues/51 - from your experience, what's currently missing to better help future users? do you think we need a headless mode that prints the URL? in case you would like to open a PR that'd be very welcome! 🙏
Author
Owner

@nleroy917 commented on GitHub (May 25, 2020):

If you need a 100% headless mode, I've actually done this before with Spotify's client credentials flow. It uses selenium to drive a headless browser, insert the client credentials, and write the tokens to the environment, so you don't ever have to deal with typing in your credentials when running scripts.

I wrote it awhile ago - so it isn't the cleanest code, but it actually worked extremely well. It's here.

<!-- gh-comment-id:633572860 --> @nleroy917 commented on GitHub (May 25, 2020): If you need a 100% headless mode, I've actually done this before with Spotify's client credentials flow. It uses selenium to drive a headless browser, insert the client credentials, and write the tokens to the environment, so you don't ever have to deal with typing in your credentials when running scripts. I wrote it awhile ago - so it isn't the cleanest code, but it actually worked extremely well. It's [here](https://github.com/NLeRoy917/spotify-playlist-analyzer/blob/master/lib/authorize.py).
Author
Owner

@varna9000 commented on GitHub (Jun 13, 2020):

@stephanebruckert

from your experience, what's currently missing to better help future users? do you think we need a headless mode that prints the URL? in case you would like to open a PR that'd be very welcome!

I would imagine, most people don't use headless installs but printing of the url would be of a huge help. Not sure I can contribute with a PR but will try.

@NLeRoy917 nice! I'll look into it.

<!-- gh-comment-id:643586700 --> @varna9000 commented on GitHub (Jun 13, 2020): @stephanebruckert > from your experience, what's currently missing to better help future users? do you think we need a headless mode that prints the URL? in case you would like to open a PR that'd be very welcome! I would imagine, most people don't use headless installs but printing of the url would be of a huge help. Not sure I can contribute with a PR but will try. @NLeRoy917 nice! I'll look into it.
Author
Owner

@stephanebruckert commented on GitHub (Jun 13, 2020):

In fact this is how it was working in version 2.10.0; the URL was printed and it was prompting for the resulting URL. PR https://github.com/plamere/spotipy/pull/243 was merged to make that process automatic

Now I see 3 solutions for the headless mode:

  • use 2.10.0: pip3 install 'spotipy~=2.10.0'

  • keep the current version 2.12.0 and set a REDIRECT_URI without any port, it will "magically" prompt for the URL (exactly what you need). More info in this PR https://github.com/plamere/spotipy/pull/477 and in the doc:

    If your request URI is set to any other URI you will need to manually copy the URI from the browser’s address bar and paste it into the terminal/console where your Spotipy script is running (Spotipy will instruct you to do so).

  • (ideally) we could bring the manual mode back by adding a headless parameter (defaults to False) in the SpotifyOAuth constructor: github.com/plamere/spotipy@587baec9b9/spotipy/oauth2.py (L200) and then check that it is not false in this condition github.com/plamere/spotipy@587baec9b9/spotipy/oauth2.py (L373) such as:

     if not self.headless or redirect_host in ("127.0.0.1", "localhost") and redirect_info.scheme == "http":
    
<!-- gh-comment-id:643589939 --> @stephanebruckert commented on GitHub (Jun 13, 2020): In fact this is how it was working in version 2.10.0; the URL was printed and it was prompting for the resulting URL. PR https://github.com/plamere/spotipy/pull/243 was merged to make that process automatic Now I see 3 solutions for the headless mode: - use 2.10.0: `pip3 install 'spotipy~=2.10.0'` - keep the current version 2.12.0 and set a `REDIRECT_URI` without any port, it will "magically" prompt for the URL (exactly what you need). More info in this PR https://github.com/plamere/spotipy/pull/477 and in the [doc](https://spotipy.readthedocs.io/en/2.12.0/): > If your request URI is set to any other URI you will need to manually copy the URI from the browser’s address bar and paste it into the terminal/console where your Spotipy script is running (Spotipy will instruct you to do so). - (ideally) we could bring the manual mode back by adding a `headless` parameter (defaults to `False`) in the `SpotifyOAuth` constructor: https://github.com/plamere/spotipy/blob/587baec9b95da6c45e45f0f8e5b2577bda780980/spotipy/oauth2.py#L200 and then check that it is not false in this condition https://github.com/plamere/spotipy/blob/587baec9b95da6c45e45f0f8e5b2577bda780980/spotipy/oauth2.py#L373 such as: ```python if not self.headless or redirect_host in ("127.0.0.1", "localhost") and redirect_info.scheme == "http": ```
Author
Owner

@smalls12 commented on GitHub (Jun 21, 2020):

I'm bumping into the same issue now re: headless mode.

Should I make make a separate PR to discuss and possibly implement your ideal solution above?

<!-- gh-comment-id:647192018 --> @smalls12 commented on GitHub (Jun 21, 2020): I'm bumping into the same issue now re: headless mode. Should I make make a separate PR to discuss and possibly implement your ideal solution above?
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#294
No description provided.