Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] BadCredentials #277

Open
hazycora opened this issue Jul 26, 2024 · 34 comments
Open

[BUG] BadCredentials #277

hazycora opened this issue Jul 26, 2024 · 34 comments
Labels
bug Something isn't working

Comments

@hazycora
Copy link

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 (3b46fe5)
@hazycora hazycora added the bug Something isn't working label Jul 26, 2024
@hazycora
Copy link
Author

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
Copy link
Owner

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

@hazycora
Copy link
Author

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

@shayanmaher
Copy link

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
Copy link

pdtan commented Jul 29, 2024

I also got this error sometimes

@kokarare1212 kokarare1212 pinned this issue Jul 29, 2024
@shayanmaher
Copy link

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
Copy link

@kokarare1212 I've started to encounter this too.

@IVIanuu
Copy link

IVIanuu commented Jul 29, 2024

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

@phuketbinaryt
Copy link

Same issue here with the bad credentials.

@dylenthomas
Copy link

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
Copy link

clemwo commented Jul 29, 2024

I also have this issue on a windows machine.

It's also being discussed in the librespot project directly here : librespot-org/librespot#1308

Feels like spotify changed something on their end maybe?

@rgon
Copy link

rgon commented Jul 30, 2024

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
Copy link
Author

hazycora commented Aug 2, 2024

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
Copy link

Happening to me as well.

@kokarare1212
Copy link
Owner

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

@staniel359
Copy link

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

@realCmdData
Copy link

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
Copy link

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
Copy link

pdtan commented Aug 8, 2024

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

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

@kokarare1212
Copy link
Owner

kokarare1212 commented Aug 8, 2024

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
Copy link

pdtan commented Aug 8, 2024

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
Copy link

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
Copy link

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
Copy link

g42ftw commented Aug 15, 2024

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.

@clemwo
Copy link

clemwo commented Aug 15, 2024

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
Copy link

Reusable credentials do not expire.

@g42ftw
Copy link

g42ftw commented Aug 15, 2024

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"]}"

@pdtan
Copy link

pdtan commented Aug 16, 2024

@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
Copy link

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
Copy link

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
Copy link

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
Copy link

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.

@clemwo
Copy link

clemwo commented Sep 15, 2024

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.

Unfortunately this code doesn't work when run in asyncio.

I get the following error:

  File "/Users/clemens/reps/carrotify/.venv/lib/python3.12/site-packages/zeroconf/_utils/asyncio.py", line 86, in run_coro_with_timeout
    return asyncio.run_coroutine_threadsafe(aw, loop).result(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 458, in result
    raise TimeoutError()
TimeoutError

Does anyone have an idea on how to adjust the code to make it run in async?

@shuttie
Copy link

shuttie commented Sep 16, 2024

This tool helped me to generate the credentials.json: https://github.com/dspearson/librespot-auth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests