ytmusicapi: 400 Bad Request error when using oauth, but browser authentication works
Describe the bug
the ytmusic.search() command is throwing a HTTP 400: Bad Request error with the message Request contains an invalid argument, but only when using oauth. Browser authentication works fine.
Using ytmusicapi version 1.8.2
To Reproduce Steps to reproduce the behavior:
- run
ytmusicapi oauthas documented in setup - Minimal script example:
from ytmusicapi import YTMusic
conn = YTMusic("oauth.json")
search_result = conn.search(f"It's beginning to look a lot like Christmas")
Result:
Traceback (most recent call last):
File "C:\dev\ytmusic\test.py", line 4, in <module>
search_result = conn.search(f"It's beginning to look a lot like Christmas")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\ytmusic\venv\Lib\site-packages\ytmusicapi\mixins\search.py", line 183, in search
response = self._send_request(endpoint, body)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\dev\ytmusic\venv\Lib\site-packages\ytmusicapi\ytmusic.py", line 241, in _send_request
raise YTMusicServerError(message + error)
ytmusicapi.exceptions.YTMusicServerError: Server returned HTTP 400: Bad Request.
Request contains an invalid argument.
Additional context I’ve been using it for quite a while without issue, but today it stopped working, seemingly at random. Switching to browser auth has temporarily fixed it for my workflow.
Deleting oauth.json and redoing the authentication flow didn’t work either.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 15
- Comments: 26 (8 by maintainers)
I doubt this is related to family plans, likely just a coincidence.
I would suspect that something in here broke/needs to be updated due the server-side update: https://github.com/sigma67/ytmusicapi/blob/main/ytmusicapi/helpers.py
I will try comparing our requests with the current requests on the web when I find time. Unfortunately I work a full time job so it might be a few days. I certainly recognize the importance of the issue given the number of reports.
In the meantime, anyone else with the time can perform the same analysis!
People in different projects are saying that Google is removing OAuth support for YTMusic, and they are switching back to using cookies 😢 The reason it happens for some accounts and not others of course is because of A/B testing (edit: well, global rollout, technically)
https://github.com/KoljaWindeler/ytube_music_player/issues/367#issuecomment-2472397147 https://github.com/music-assistant/hass-music-assistant/issues/3138#issuecomment-2471316657 https://github.com/yt-dlp/yt-dlp/issues/11462#issuecomment-2471087887
I haven’t seen anything conclusive stating this to be true, but it seems like developers of different projects are seeing the writing on the wall, which points toward bye-bye for OAuth. Let’s hope that’s not the case…I’m not sure how to determine if it is or not.
We’re all well aware that the issue is happening, now it’s time for the open-source community to do its thing and come together and fix it. If you scroll up and read @sigma67’s message, you’ll see they are busy and some suggestions about where to look in the meantime. It’s probably some header or something that changed on one of the requests in the oauth flow. I tried looking at the API docs for OAuth 2.0 for TV and Limited-Input Device Applications but nothing stuck out as being different. Also tried going through the oauth flow in the browser and looking at the requests/responses to see if there’s anything that looks like one of the requests in the helpers.py file, but I might just be looking in the wrong place. The properties in
oauth.jsonitself seem to match exactly what’s expected according to that API document…My account is the manager of my family plan and I get the
400: Bad Requesterror when using oauth creds.it was working for me till yesterday , today even im facing this issue , sigma67 is there any fix for this?
“te”: “trailers\u001a”
I solved the issue by removing this code from the browser.json file. However, I’ve been struggling with it for hours. It was so silly. Example: (where there was a space there was the usual “te”: “trailers\u001a”.)
Common advice to make long-lasting cookies is to create them using the browser’s in-private session. This avoids accidentally messing with the session during your regular browsing activity
The cookies last quite long in my experience
i am seeing this issue as a manager of a family plan.
same issue, commenting for visibility!
guessing its a server side change that break the api
I too have started getting this, though, I can’t get the browser auth to work either.