librespot-python icon indicating copy to clipboard operation
librespot-python copied to clipboard

[BUG] BadCredentials

Open hazycora opened this issue 1 year ago • 43 comments

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):

  • OS: Arch Linux
  • Python Version: 3.12.4
  • Library Version: Last snapshot (3b46fe560ad829b976ce63e85012cff95b1e0bf3)

hazycora avatar Jul 26 '24 17:07 hazycora

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!

hazycora avatar Jul 26 '24 18:07 hazycora

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

kokarare1212 avatar Jul 27 '24 03:07 kokarare1212

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

hazycora avatar Jul 28 '24 21:07 hazycora

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

shayanmaher avatar Jul 28 '24 22:07 shayanmaher

I also got this error sometimes

pdtan avatar Jul 29 '24 02:07 pdtan

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

shayanmaher avatar Jul 29 '24 08:07 shayanmaher

@kokarare1212 I've started to encounter this too.

staniel359 avatar Jul 29 '24 14:07 staniel359

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

IVIanuu avatar Jul 29 '24 16:07 IVIanuu

Same issue here with the bad credentials.

phuketbinaryt avatar Jul 29 '24 17:07 phuketbinaryt

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?

dylenthomas avatar Jul 29 '24 17:07 dylenthomas

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?

clemwo avatar Jul 29 '24 23:07 clemwo

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

rgon avatar Jul 30 '24 07:07 rgon

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.

hazycora avatar Aug 02 '24 23:08 hazycora

Happening to me as well.

TreeOfSelf avatar Aug 02 '24 23:08 TreeOfSelf

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.

kokarare1212 avatar Aug 03 '24 00:08 kokarare1212

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

staniel359 avatar Aug 03 '24 09:08 staniel359

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 avatar Aug 07 '24 19:08 realCmdData

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...

realCmdData avatar Aug 07 '24 19:08 realCmdData

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

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

pdtan avatar Aug 08 '24 01:08 pdtan

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.

kokarare1212 avatar Aug 08 '24 07:08 kokarare1212

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

pdtan avatar Aug 08 '24 10:08 pdtan

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.

kingosticks avatar Aug 12 '24 14:08 kingosticks

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

dracarys69 avatar Aug 15 '24 12:08 dracarys69

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.

g42ftw avatar Aug 15 '24 15:08 g42ftw

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.

clemwo avatar Aug 15 '24 19:08 clemwo

Reusable credentials do not expire.

kingosticks avatar Aug 15 '24 19:08 kingosticks

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
autologin.blob="BLOB"
autologin.saved_credentials="{"USERNAME": ["USERNAME", "BLOB"]}"

g42ftw avatar Aug 15 '24 20:08 g42ftw

@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
}

pdtan avatar Aug 16 '24 02:08 pdtan

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

sertraline avatar Aug 17 '24 18:08 sertraline

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

direct-dok avatar Aug 22 '24 11:08 direct-dok