xtekky/gpt4free · error · NoValidHarFileError

Missing access token

Error message

Missing access token

What it means

During OpenaiChat's auth bootstrap, request_config (from HAR/browser) was obtained but request_config.access_token is None while cls.needs_auth is true, so NoValidHarFileError 'Missing access token' is raised (a second variant validates the token and reports it invalid). It means credentials were loaded but contained no access token at all.

Source

Thrown at g4f/Provider/needs_auth/OpenaiChat.py:1382

        if proof_token is not None:
            cls.request_config.proof_token = proof_token
        if cookies is not None:
            cls.request_config.cookies = cookies
        if api_key is not None:
            cls._create_request_args(
                cls.request_config.cookies, cls.request_config.headers
            )
            cls._set_api_key(api_key)
        else:
            try:
                cls.request_config = await get_request_config(cls.request_config, proxy)
                if cls.request_config is None:
                    cls.request_config = RequestConfig()
                cls._create_request_args(
                    cls.request_config.cookies, cls.request_config.headers
                )
                if cls.needs_auth and cls.request_config.access_token is None:
                    raise NoValidHarFileError(f"Missing access token")
                if not cls._set_api_key(cls.request_config.access_token):
                    raise NoValidHarFileError(
                        f"Access token is not valid: {cls.request_config.access_token}"
                    )
            except NoValidHarFileError:
                if has_nodriver:
                    if cls.request_config.access_token is None:
                        yield RequestLogin(
                            cls.label, os.environ.get("G4F_LOGIN_URL", "")
                        )
                        await cls.nodriver_auth(proxy)
                else:
                    raise

    @classmethod
    async def nodriver_auth(cls, proxy: str = None):
        async with get_nodriver_session(proxy=proxy) as browser:
            page = await browser.get(cls.url)

View on GitHub (pinned to 973504e177)

Solutions

  1. Re-capture credentials while fully logged in, ensuring requests to /api/auth/session (which return accessToken) are included in the HAR
  2. Install nodriver (g4f[nodriver]) so the RequestLogin + nodriver_auth() fallback can acquire the token interactively
  3. Update g4f in case token extraction from the auth source changed
  4. Clear the cached request config so fresh credentials are acquired instead of the token-less cache

Example fix

# before
# HAR without token -> NoValidHarFileError: Missing access token

# after
# 1. log in fully in the browser, then export HAR including /api/auth/session
# 2. or install the fallback:
$ pip install 'g4f[nodriver]'
# first call then opens RequestLogin and performs nodriver_auth()
Defensive patterns

Strategy: validation

Validate before calling

config = await get_request_config(None, proxy)
if config is not None and config.access_token is None and OpenaiChat.needs_auth:
    # no token in capture; fall back to nodriver login now rather than failing mid-request
    await OpenaiChat.nodriver_auth(proxy)

Type guard

def config_has_token(config) -> bool:
    return config is not None and getattr(config, 'access_token', None) is not None

Try / catch

from g4f.errors import NoValidHarFileError
try:
    resp = await client.chat.completions.create(model=..., messages=msgs)
except NoValidHarFileError as e:
    if 'Missing access token' in str(e):
        await OpenaiChat.nodriver_auth(proxy)  # interactive login fallback
        resp = await client.chat.completions.create(model=..., messages=msgs)
    else:
        raise

Prevention

When it happens

Trigger: get_request_config() returns a config whose access_token is None — e.g. a HAR capture without an accessToken request, a partially completed browser login, or an auth source that yielded cookies but no token. If nodriver is available the code falls back to interactive login; otherwise the error propagates.

Common situations: HAR exported before the app fetched its access token; browser session logged out mid-capture; g4f's token extraction failing after an OpenAI change; environments without nodriver where no fallback exists.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/08ceef822383ad9c. Report an issue: GitHub.