{"record":{"id":"08ceef822383ad9c","repo":"xtekky/gpt4free","slug":"missing-access-token","errorCode":null,"errorMessage":"Missing access token","messagePattern":"Missing access token","errorType":"exception","errorClass":"NoValidHarFileError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/OpenaiChat.py","lineNumber":1382,"sourceCode":"        if proof_token is not None:\n            cls.request_config.proof_token = proof_token\n        if cookies is not None:\n            cls.request_config.cookies = cookies\n        if api_key is not None:\n            cls._create_request_args(\n                cls.request_config.cookies, cls.request_config.headers\n            )\n            cls._set_api_key(api_key)\n        else:\n            try:\n                cls.request_config = await get_request_config(cls.request_config, proxy)\n                if cls.request_config is None:\n                    cls.request_config = RequestConfig()\n                cls._create_request_args(\n                    cls.request_config.cookies, cls.request_config.headers\n                )\n                if cls.needs_auth and cls.request_config.access_token is None:\n                    raise NoValidHarFileError(f\"Missing access token\")\n                if not cls._set_api_key(cls.request_config.access_token):\n                    raise NoValidHarFileError(\n                        f\"Access token is not valid: {cls.request_config.access_token}\"\n                    )\n            except NoValidHarFileError:\n                if has_nodriver:\n                    if cls.request_config.access_token is None:\n                        yield RequestLogin(\n                            cls.label, os.environ.get(\"G4F_LOGIN_URL\", \"\")\n                        )\n                        await cls.nodriver_auth(proxy)\n                else:\n                    raise\n\n    @classmethod\n    async def nodriver_auth(cls, proxy: str = None):\n        async with get_nodriver_session(proxy=proxy) as browser:\n            page = await browser.get(cls.url)","sourceCodeStart":1364,"sourceCodeEnd":1400,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/OpenaiChat.py#L1364-L1400","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-capture credentials while fully logged in, ensuring requests to /api/auth/session (which return accessToken) are included in the HAR","Install nodriver (g4f[nodriver]) so the RequestLogin + nodriver_auth() fallback can acquire the token interactively","Update g4f in case token extraction from the auth source changed","Clear the cached request config so fresh credentials are acquired instead of the token-less cache"],"exampleFix":"# before\n# HAR without token -> NoValidHarFileError: Missing access token\n\n# after\n# 1. log in fully in the browser, then export HAR including /api/auth/session\n# 2. or install the fallback:\n$ pip install 'g4f[nodriver]'\n# first call then opens RequestLogin and performs nodriver_auth()","handlingStrategy":"validation","validationCode":"config = await get_request_config(None, proxy)\nif config is not None and config.access_token is None and OpenaiChat.needs_auth:\n    # no token in capture; fall back to nodriver login now rather than failing mid-request\n    await OpenaiChat.nodriver_auth(proxy)","typeGuard":"def config_has_token(config) -> bool:\n    return config is not None and getattr(config, 'access_token', None) is not None","tryCatchPattern":"from g4f.errors import NoValidHarFileError\ntry:\n    resp = await client.chat.completions.create(model=..., messages=msgs)\nexcept NoValidHarFileError as e:\n    if 'Missing access token' in str(e):\n        await OpenaiChat.nodriver_auth(proxy)  # interactive login fallback\n        resp = await client.chat.completions.create(model=..., messages=msgs)\n    else:\n        raise","preventionTips":["Capture HAR only after full login so /api/auth/session (accessToken) is included","Install g4f[nodriver] to enable the automatic login fallback","Clear token-less cached configs so fresh credentials are fetched"],"tags":["openai-chat","auth","har","access-token"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}