NanmiCoder/MediaCrawler · error · ValueError

[BilibiliLogin.begin] Invalid Login Type Currently only supp

Error message

[BilibiliLogin.begin] Invalid Login Type Currently only supported qrcode or phone or cookie ...

What it means

ValueError from BilibiliLogin.begin when the global config.LOGIN_TYPE is not one of the three supported literals 'qrcode', 'phone', 'cookie'. Login dispatch is a simple if/elif over the config string, and any other value aborts login before any browser interaction.

Source

Thrown at media_platform/bilibili/login.py:64

                 cookie_str: str = ""
                 ):
        config.LOGIN_TYPE = login_type
        self.browser_context = browser_context
        self.context_page = context_page
        self.login_phone = login_phone
        self.cookie_str = cookie_str

    async def begin(self):
        """Start login bilibili"""
        utils.logger.info("[BilibiliLogin.begin] Begin login Bilibili ...")
        if config.LOGIN_TYPE == "qrcode":
            await self.login_by_qrcode()
        elif config.LOGIN_TYPE == "phone":
            await self.login_by_mobile()
        elif config.LOGIN_TYPE == "cookie":
            await self.login_by_cookies()
        else:
            raise ValueError(
                "[BilibiliLogin.begin] Invalid Login Type Currently only supported qrcode or phone or cookie ...")

    @retry(stop=stop_after_attempt(600), wait=wait_fixed(1), retry=retry_if_result(lambda value: value is False))
    async def check_login_state(self) -> bool:
        """
            Check if the current login status is successful and return True otherwise return False
            retry decorator will retry 20 times if the return value is False, and the retry interval is 1 second
            if max retry times reached, raise RetryError
        """
        current_cookie = await self.browser_context.cookies()
        _, cookie_dict = utils.convert_cookies(current_cookie)
        if cookie_dict.get("SESSDATA", "") or cookie_dict.get("DedeUserID"):
            return True
        return False

    async def login_by_qrcode(self):
        """login bilibili website and keep webdriver login state"""
        utils.logger.info("[BilibiliLogin.login_by_qrcode] Begin login bilibili by qrcode ...")

View on GitHub (pinned to d6f7c5bb90)

Solutions

  1. Set LOGIN_TYPE to exactly 'qrcode', 'phone', or 'cookie' (lowercase) in config.
  2. For headless/automated runs prefer 'cookie' with a valid cookie string, since qrcode needs a human scan.
  3. Validate the value at startup against the allowed set and fail fast with a clear message.

Example fix

# before
config.LOGIN_TYPE = 'qr'

# after
config.LOGIN_TYPE = 'qrcode'
Defensive patterns

Strategy: validation

Validate before calling

from config import config
assert config.LOGIN_TYPE in {'qrcode', 'phone', 'cookie'}, f'bad LOGIN_TYPE: {config.LOGIN_TYPE!r}'

Type guard

def is_valid_login_type(t: str) -> bool:
    return t in {'qrcode', 'phone', 'cookie'}

Try / catch

try:
    await BilibiliLogin(...).begin()
except ValueError as e:
    if 'Invalid Login Type' in str(e):
        raise SystemExit('set LOGIN_TYPE to qrcode, phone, or cookie in config') from e

Prevention

When it happens

Trigger: LOGIN_TYPE set to 'qr', 'sms', 'Cookie' (capitalized), 'password', or left empty in config/base_config.py or env override; config value read from a .env that quotes or spaces the value.

Common situations: Editing the sample config and guessing at allowed values; CI passing a different LOGIN_TYPE than local runs; version drift where supported login methods changed.

Related errors


AI-assisted analysis of NanmiCoder/MediaCrawler@d6f7c5bb90 (2026-08-15). Data as JSON: /api/errors/5c764f3b37c0e5de. Report an issue: GitHub.