arsaboo / beets-youtube

A beets plugin to get metadata from Youtube
MIT License
3 stars 0 forks source link

Bug: Invalid Header Type Error in beets-youtube Plugin Causing Crash #9

Open lavish440 opened 3 months ago

lavish440 commented 3 months ago

Description

The application crashes with an InvalidHeader exception when trying to initialize the beets-youtube plugin. The error occurs because a header value is provided as an integer instead of a string or bytes.

Steps to Reproduce

  1. Install and configure the beets-youtube plugin as suggested in the README.
  2. Generate oauth.json using the command ytmusicapi oauth.
  3. Move oauth.json to $HOME/.config/beets/.
  4. Run the plugin with the command beet youtube or try importing any album or single.

Expected Behavior

The beets-youtube plugin should be initialized without errors and beets should continue to run.

Actual Behavior

The application crashes with the following traceback:

Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/bin/beet", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/ui/__init__.py", line 1301, in main
    _raw_main(args)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/ui/__init__.py", line 1284, in _raw_main
    subcommands, plugins, lib = _setup(options, lib)
                                ^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/ui/__init__.py", line 1150, in _setup
    plugin_types_album = plugins.types(library.Album)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/plugins.py", line 337, in types
    for plugin in find_plugins():
                  ^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/plugins.py", line 307, in find_plugins
    _instances[cls] = cls()
                      ^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beetsplug/youtube.py", line 50, in __init__
    self.yt = YTMusic(os.path.join(config.config_dir(), 'oauth.json'))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/ytmusicapi/ytmusic.py", line 101, in __init__
    self.headers.update(get_visitor_id(self._send_get_request))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/ytmusicapi/helpers.py", line 37, in get_visitor_id
    response = request_func(YTM_DOMAIN)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/ytmusicapi/ytmusic.py", line 150, in _send_get_request
    response = self._session.get(url,
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/sessions.py", line 575, in request
    prep = self.prepare_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/sessions.py", line 486, in prepare_request
    p.prepare(
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/models.py", line 369, in prepare
    self.prepare_headers(headers)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/models.py", line 491, in prepare_headers
    check_header_validity(header)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/utils.py", line 1040, in check_header_validity
    _validate_header_part(header, value, 1)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/requests/utils.py", line 1049, in _validate_header_part
    raise InvalidHeader(
requests.exceptions.InvalidHeader: Header part (1716149989) from ('expires_at', 1716149989) must be of type str or bytes, not <class 'int'>

Environment

Additional Information

It seems that the expires_at header value is being passed as an integer. This should be converted to a string or bytes before being used as a header.

Suggested Fix

Ensure that the expires_at header value is of type str or bytes before updating the headers in ytmusicapi.

arsaboo commented 3 months ago

Are you sure you copied the file correctly....check the contents of the file. It is working fine for me.

lavish440 commented 3 months ago

@arsaboo I am sure that I have copied the file correctly. Here is the output of ls -lah ~/.config/beets:

Screenshot_2024-05-21-07-16-28-834_com.termux-edit.jpg

Here are the contents of the file:

{
 "scope": "https://www.googleapis.com/auth/youtube",
 "token_type": "Bearer",
 "access_token": "access_token_value_here",
 "refresh_token": "refresh_token_value_here",
 "expires_at": 1716149989,
 "expires_in": 86245
}
arsaboo commented 3 months ago

The token looks fine. Can you try manually adding quotes around expires_at value?

lavish440 commented 3 months ago

I manually added quotes to both expires_in and expires_at but it still doesn't work.

Here is the traceback:

Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/bin/beet", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/ui/__init__.py", line 1301, in main
    _raw_main(args)
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/ui/__init__.py", line 1284, in _raw_main
    subcommands, plugins, lib = _setup(options, lib)
                                ^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/ui/__init__.py", line 1150, in _setup
    plugin_types_album = plugins.types(library.Album)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/plugins.py", line 337, in types
    for plugin in find_plugins():
                  ^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beets/plugins.py", line 307, in find_plugins
    _instances[cls] = cls()
                      ^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/beetsplug/youtube.py", line 50, in __init__
    self.yt = YTMusic(os.path.join(config.config_dir(), 'oauth.json'))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/ytmusicapi/ytmusic.py", line 127, in __init__
    self.sapisid = sapisid_from_cookie(cookie)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/ytmusicapi/helpers.py", line 48, in sapisid_from_cookie
    cookie.load(raw_cookie.replace("\"", ""))
                ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'
lavish440 commented 3 months ago

@arsaboo

arsaboo commented 3 months ago

I am still not able to replicate the bug.

lavish440 commented 3 months ago

How does the oauth.json look for you?

arsaboo commented 1 month ago
{
 "scope": "https://www.googleapis.com/auth/youtube",
 "token_type": "Bearer",
 "access_token": "ya29.REDACTED-tDgtiQQ0185",
 "refresh_token": "1//01s52REDACTEDqdITUSw",
 "expires_at": 1721469696,
 "expires_in": 81339
}