{"record":{"id":"18f62dcea8ca18fb","repo":"xtekky/gpt4free","slug":"no-access-token-found-in-har-files","errorCode":null,"errorMessage":"No access token found in .har files","messagePattern":"No access token found in \\.har files","errorType":"exception","errorClass":"NoValidHarFileError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/MicrosoftDesigner.py","lineNumber":168,"sourceCode":"def readHAR(url: str) -> tuple[str, str]:\n    api_key = None\n    user_agent = None\n    for path in get_har_files():\n        with open(path, \"rb\") as file:\n            try:\n                harFile = json.loads(file.read())\n            except json.JSONDecodeError:\n                # Error: not a HAR file!\n                continue\n            for v in harFile[\"log\"][\"entries\"]:\n                if v[\"request\"][\"url\"].startswith(url):\n                    v_headers = get_headers(v)\n                    if \"authorization\" in v_headers:\n                        api_key = v_headers[\"authorization\"].split(maxsplit=1).pop()\n                    if \"user-agent\" in v_headers:\n                        user_agent = v_headers[\"user-agent\"]\n    if api_key is None:\n        raise NoValidHarFileError(\"No access token found in .har files\")\n\n    return api_key, user_agent\n\n\nasync def get_access_token_and_user_agent(url: str, proxy: str = None):\n    browser, stop_browser = await get_nodriver(proxy=proxy, user_data_dir=\"designer\")\n    try:\n        page = await browser.get(url)\n        user_agent = await page.evaluate(\"navigator.userAgent\", return_by_value=True)\n        access_token = None\n        while access_token is None:\n            access_token = await page.evaluate(\n                \"\"\"\n                (() => {\n                    for (var i = 0; i < localStorage.length; i++) {\n                        try {\n                            item = JSON.parse(localStorage.getItem(localStorage.key(i)));\n                            if (item.credentialType == \"AccessToken\" ","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/MicrosoftDesigner.py#L150-L186","documentation":"MicrosoftDesigner.readHAR() scans all .har files on disk for requests whose URL starts with the designer endpoint and extracts the Authorization bearer token; if no HAR file yields one, NoValidHarFileError is raised. It means the provider could not find designer credentials in any exported browser capture.","triggerScenarios":"readHAR(url) runs (via _create_auth_from_har or similar) and every HAR file either fails JSON parsing, contains no matching URL entries, or matching entries lack an authorization header.","commonSituations":"HAR exported before signing into designer services so no auth header was captured; wrong URL prefix captured (different Microsoft endpoint); HAR files missing from the expected directory; expired token not the issue here — none was found at all.","solutions":["Re-export the HAR: log into the Designer/Bing Image Creator site first, then record the session and export the .har file","Confirm the HAR contains a request to the designer URL with an Authorization: Bearer ... header (search the JSON for 'authorization')","Place the .har file where g4f looks (get_har_files() paths) and ensure it is valid JSON","Alternatively rely on the nodriver flow get_access_token_and_user_agent() which reads the token from browser localStorage instead of HAR"],"exampleFix":"# before\n# HAR exported while logged out -> NoValidHarFileError\n\n# after\n# 1. open Chrome, sign in at the designer site\n# 2. DevTools -> Network -> record -> refresh page -> export HAR\n# 3. save to the g4f HAR directory, then:\napi_key, user_agent = readHAR(designer_url)","handlingStrategy":"validation","validationCode":"import json\ndef har_has_designer_token(path: str, url_prefix: str) -> bool:\n    try:\n        har = json.load(open(path, 'rb'))\n    except (json.JSONDecodeError, OSError):\n        return False\n    return any(\n        e['request']['url'].startswith(url_prefix)\n        and any(h[0].lower() == 'authorization' for h in [(k, v) for k, v in e['request'].get('headers', {}).items()])\n        for e in har.get('log', {}).get('entries', [])\n    )","typeGuard":"def har_contains_auth(har: dict, url_prefix: str) -> bool:\n    for e in har.get('log', {}).get('entries', []):\n        if e.get('request', {}).get('url', '').startswith(url_prefix):\n            headers = {k.lower(): v for k, v in e['request'].get('headers', {}).items()}\n            if 'authorization' in headers:\n                return True\n    return False","tryCatchPattern":"from g4f.errors import NoValidHarFileError\ntry:\n    api_key, ua = readHAR(designer_url)\nexcept NoValidHarFileError:\n    api_key, ua = await get_access_token_and_user_agent(designer_url, proxy)  # nodriver fallback","preventionTips":["Export HAR only while fully signed in to the designer service","Verify the HAR contains an Authorization header before relying on it","Prefer the nodriver token flow in automated environments"],"tags":["microsoft-designer","har","auth","credentials"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}