kokarare1212 / librespot-python

Open Source Spotify Client
http://librespot-python.rtfd.io
Apache License 2.0
220 stars 43 forks source link

[BUG] BadCredentials #277

Open hazycora opened 1 month ago

hazycora commented 1 month ago

Describe the bug Login fails on some accounts on some devices. My current guess is they're doing some kind of A/B test to phase out something this relies on? Maybe the protocol version needs to be updated? Occurs not just here but also on the separate librespot-js project and librespot-java. The bug is not with some library-specific logic but with a change to Spotify- a fix here will likely also be able to be applied everywhere else.

To Reproduce

from librespot.core import Session

session = Session.Builder() \
    .user_pass("username", "password") \
    .create()

Expected behavior The login should be successful.

Screenshots

Traceback (most recent call last):
  File "/home/hazy/tmp_a/test.py", line 7, in <module>
    .create()
     ^^^^^^^^
  File "/home/hazy/tmp_a/venv/lib/python3.12/site-packages/librespot/core.py", line 1634, in create
    session.authenticate(self.login_credentials)
  File "/home/hazy/tmp_a/venv/lib/python3.12/site-packages/librespot/core.py", line 939, in authenticate
    self.__authenticate_partial(credential, False)
  File "/home/hazy/tmp_a/venv/lib/python3.12/site-packages/librespot/core.py", line 1350, in __authenticate_partial
    raise Session.SpotifyAuthenticationException(ap_login_failed)
librespot.core.Session.SpotifyAuthenticationException: BadCredentials

Client Information (please complete the following information):

hazycora commented 1 month ago

If it can't be reproduced, I have a server that has this problem, and I'm up for helping in any way I can!

kokarare1212 commented 1 month ago

I don't know the details, I suspect that your IP address has been blacklisted due to the number of login attempts?

hazycora commented 1 month ago

given that this occurred the only time I had ever tried on this server, no I don't believe that

shayanmaher commented 1 month ago

I don't know the details, I suspect that your IP address has been blacklisted due to the number of login attempts?

also i have this issue for login auth with this error raise Session.SpotifyAuthenticationException(ap_login_failed) librespot.core.Session.SpotifyAuthenticationException: BadCredentials

pdtan commented 1 month ago

I also got this error sometimes

shayanmaher commented 1 month ago

I also got this error sometimes

i solve this problem it belongs to ip of datacenters which in blacklist popular Datacenters like hetzener,...etc has this problem you can solve this issue by use a fresh ip

staniel359 commented 1 month ago

@kokarare1212 I've started to encounter this too.

IVIanuu commented 1 month ago

I also encounter this issue since today.. Any fix?

phuketbinaryt commented 1 month ago

Same issue here with the bad credentials.

dylenthomas commented 1 month ago

This just started happening to me today too, might be an issue with spotify's client? I tried on a MacBook with latest MacOS and my linux desktop, both having same issue. I also tried with the latest install of librespot, that was working yesterday.

Since this is happening to so many people they might have an issue with their api right now?

clemwo commented 1 month ago

I also have this issue on a windows machine.

It's also being discussed in the librespot project directly here : https://github.com/librespot-org/librespot/issues/1308

Feels like spotify changed something on their end maybe?

rgon commented 1 month ago

i solve this problem it belongs to ip of datacenters which in blacklist popular Datacenters like hetzener,...

@shayanmaher happening to me on a residential IP. Definitely not data center blacklisting, but rather seems like a buggy rolled-out deploy in their end or rather some of their pesky A/B API testing

hazycora commented 1 month ago

I also got this error sometimes

i solve this problem it belongs to ip of datacenters which in blacklist popular Datacenters like hetzener,...etc has this problem you can solve this issue by use a fresh ip

this is not true. happens on all residential IPs ive tried now. that was absolutely just part of the rollout.

TreeOfSelf commented 1 month ago

Happening to me as well.

kokarare1212 commented 1 month ago

It appears that login using username and password has been discontinued. https://github.com/librespot-org/librespot-java/issues/921#issuecomment-2258440907 Please use other login methods (e.g. zeroconf) or PR a new login method instead.

staniel359 commented 1 month ago

@kokarare1212 Could you then provide the instructions on how to use Zeroconf instead of Session?

realCmdData commented 1 month ago

Same issue for me, even changing IP's through a VPN doesn't work.

  File "C:\Users\myusername\AppData\Local\Programs\Python\Python310\lib\site-packages\librespot\core.py", line 1350, in __authenticate_partial
    raise Session.SpotifyAuthenticationException(ap_login_failed)
librespot.core.Session.SpotifyAuthenticationException: BadCredentials
realCmdData commented 1 month ago

Same issue for me, even changing IP's through a VPN doesn't work.

  File "C:\Users\myusername\AppData\Local\Programs\Python\Python310\lib\site-packages\librespot\core.py", line 1350, in __authenticate_partial
    raise Session.SpotifyAuthenticationException(ap_login_failed)
librespot.core.Session.SpotifyAuthenticationException: BadCredentials

UPDATE:

Got it to work... Somehow... Still not quite sure how but I tried restarting my PC and deactivating my VPN, and it worked. Perhaps through trial and error? Or Spotify is blocking CLI requests that come from known VPN servers? I have no idea...

pdtan commented 1 month ago

Please use other login methods (e.g. zeroconf)

Please provide the instructions on how to use Zeroconf instead of Session?

kokarare1212 commented 1 month ago

It is incomplete but should work. See the original librespot-java for details.

Sample code can be found here. Sessions within zeroconf should be accessible via these: zeroconf._ZeroconfServer__session

Remember I have very little time to devote to these projects.

pdtan commented 1 month ago

https://github.com/kokarare1212/librespot-python#use-zeroconf-for-login zeroconf._ZeroconfServer__session

Very unclear. btw, I don't see _ZeroconfServer__session:

[INFO     09:58:35] Zeroconf HTTP server started successfully on port 58172!
[DEBUG    09:58:35] Using selector: EpollSelector
{'_ZeroconfServer__inner': <librespot.zeroconf.ZeroconfServer.Inner object at 0xffff8c9e2760>, '_ZeroconfServer__keys': <librespot.crypto.DiffieHellman object at 0xffff8c991f10>, '_ZeroconfServer__runner': <librespot.zeroconf.ZeroconfServer.HttpRunner object at 0xffff8c4a09d0>, '_ZeroconfServer__zeroconf': <zeroconf._core.Zeroconf object at 0xffff8c4a0c10>, '_ZeroconfServer__service_info': ServiceInfo(type='_spotify-connect._tcp.local.', name='librespot-python._spotify-connect._tcp.local.', addresses=[b'@nE\x8d'], port=58172, weight=0, priority=0, server='mail.', properties={b'CPath': b'/', b'VERSION': b'1.0', b'STACK': b'SP'}, interface_index=None)}
[DEBUG    09:58:36] Using selector: EpollSelector
kingosticks commented 3 weeks ago

https://github.com/kokarare1212/librespot-python#use-zeroconf-for-login zeroconf._ZeroconfServer__session

Very unclear. btw, I don't see _ZeroconfServer__session:

[INFO     09:58:35] Zeroconf HTTP server started successfully on port 58172!
[DEBUG    09:58:35] Using selector: EpollSelector
{'_ZeroconfServer__inner': <librespot.zeroconf.ZeroconfServer.Inner object at 0xffff8c9e2760>, '_ZeroconfServer__keys': <librespot.crypto.DiffieHellman object at 0xffff8c991f10>, '_ZeroconfServer__runner': <librespot.zeroconf.ZeroconfServer.HttpRunner object at 0xffff8c4a09d0>, '_ZeroconfServer__zeroconf': <zeroconf._core.Zeroconf object at 0xffff8c4a0c10>, '_ZeroconfServer__service_info': ServiceInfo(type='_spotify-connect._tcp.local.', name='librespot-python._spotify-connect._tcp.local.', addresses=[b'@nE\x8d'], port=58172, weight=0, priority=0, server='mail.', properties={b'CPath': b'/', b'VERSION': b'1.0', b'STACK': b'SP'}, interface_index=None)}
[DEBUG    09:58:36] Using selector: EpollSelector

Forgive me for jumping in, but you have to actually use the zeroconf server to auth a client before a session will be created. Given the log you provided, it doesn't look like you did that.

dracarys69 commented 3 weeks ago

idk about you guys i can still login fine with email and password and whenever i get BadCredentials issue i delete the credentials json and change my account password and it starts working again. tho its been a while since i had that issue

g42ftw commented 3 weeks ago

Login with username/password via .user_pass() method is no longer possible: https://github.com/librespot-org/librespot/issues/1308#issuecomment-2276196094

Please provide the instructions on how to use Zeroconf instead of Session?

@pdtan you may use this very sketchy code to generate credentials file to use further. Notice that device running the script should be on the same network as a desktop client in order to transfer playback or/and grab session

Use .stored_file() method instead or .user_pass() on session builder stage to load login credentials from local file:

session = Session.Builder() \
    .stored_file() \
    .create()

It should use credentials.json from current working directory by default.

clemwo commented 3 weeks ago

Login with username/password via .user_pass() method is no longer possible: librespot-org/librespot#1308 (comment)

Please provide the instructions on how to use Zeroconf instead of Session?

@pdtan you may use this very sketchy code to generate credentials file to use further. Notice that device running the script should be on the same network as a desktop client in order to transfer playback or/and grab session

Use .stored_file() method instead or .user_pass() on session builder stage to load login credentials from local file:

session = Session.Builder() \
    .stored_file() \
    .create()

It should use credentials.json from current working directory by default.

Cool beans, that works.

Question: do the credentials expire? I want to run librespot in a docker container and maybe transfering playback once would be fine. But if the token expired every few days that would be an issue for me.

kingosticks commented 3 weeks ago

Reusable credentials do not expire.

g42ftw commented 3 weeks ago

Question: do the credentials expire?

@clemwo they don't expire, unless you change your password or Spotify does. Successful login with zeroconf should always return the same long-live credentials that .user_pass always did (username+credentials blob).

The Spotify desktop client stores account login data in the client configuration file in a similar way:

.config/Spotify/prefs ```ini autologin.blob="BLOB" autologin.saved_credentials="{"USERNAME": ["USERNAME", "BLOB"]}" ```
pdtan commented 3 weeks ago

@clemwo thanks, but I use credentials.json from start, sometimes BadCredentials still happended, not usual. If using zeroconf to only create credentials.json, I actually don't need it.

RuntimeError IN (test.py, LINE 141 "session = Session.Builder().stored_file().create()"): login_failed {
  error_code: BadCredentials
}
sertraline commented 3 weeks ago

On Windows (this works on Linux) trying to use zeroconf results in the following:

WARNING:zeroconf:Error with socket 696 (('0.0.0.0', 5353))): [WinError 87] The parameter is incorrect
Traceback (most recent call last):
  File "C:\Users\sertraline\AppData\Local\Programs\Python\Python312\Lib\asyncio\proactor_events.py", line 577, in _loop_reading
    self._read_fut = self._loop._proactor.recvfrom(self._sock,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sertraline\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_events.py", line 509, in recvfrom
    self._register_with_iocp(conn)
  File "C:\Users\sertraline\AppData\Local\Programs\Python\Python312\Lib\asyncio\windows_events.py", line 709, in _register_with_iocp
    _overlapped.CreateIoCompletionPort(obj.fileno(), self._iocp, 0, 0)
OSError: [WinError 87] The parameter is incorrect
direct-dok commented 2 weeks ago

Hello everyone. Tell me if the librespot.core problem has been solved.Session.SpotifyAuthenticationException: BadCredentials Can someone tell me what needs to be done? Thanks for any hints

sertraline commented 1 week ago

I don't quite get how to generate the credentials. Quick glance at zeroconf.py tells me that I somehow need to get into handle_add_user to generate the credentials. I have tried to reopen the spotify client, log out and log in back but the add_user request just never happens, so I'm clueless on how to actually use it.

Zeroconf HTTP server started successfully on port 35710!
Handling request: GET, /?action=getInfo, HTTP/1.1, headers: {'Accept-Encoding': 'gzip', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Keep-Alive': '0', 'Host': '127.0.1.1', 'User-Agent': 'Spotify/124200000 Linux/0 (PC desktop)'}
Handling request: GET, /?action=getInfo, HTTP/1.1, headers: {'Accept-Encoding': 'gzip', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Keep-Alive': '0', 'Host': '127.0.1.1', 'User-Agent': 'Spotify/124200000 Linux/0 (PC desktop)'}
Handling request: GET, /?action=getInfo, HTTP/1.1, headers: {'Accept-Encoding': 'gzip', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Keep-Alive': '0', 'Host': '127.0.1.1', 'User-Agent': 'Spotify/124200000 Linux/0 (PC desktop)'}
Handling request: GET, /?action=getInfo&version=2.7.1, HTTP/1.1, headers: {'Accept-Encoding': 'gzip', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Keep-Alive': '0', 'Host': '127.0.1.1', 'User-Agent': 'Spotify/124200000 Linux/0 (PC desktop)'}
kingosticks commented 1 week ago

Select the running librespot instance in your Spotify client. You should find it in the list of Spotify Connect devices in Spotify's client. That device menu is at the bottom of their app on the currently playing bar, it has a speaker/computer icon.

Selecting the librespot instance will send the required auth data to the Zeroconf server where it can be stored and reused indefinitely.