{"record":{"id":"a116c4b9bf2da15b","repo":"xtekky/gpt4free","slug":"invalid-secrets","errorCode":null,"errorMessage":"Invalid secrets","messagePattern":"Invalid secrets","errorType":"validation","errorClass":"MissingAuthError","httpStatus":403,"severity":"critical","filePath":"g4f/Provider/needs_auth/Grok.py","lineNumber":214,"sourceCode":"        )\n\n        async with StreamSession(**auth_result.get_dict()) as session:\n            payload = await cls._prepare_payload(model, prompt)\n\n            # Add voice mode support flag (for future use)\n            if kwargs.get(\"enable_voice\", False):\n                payload[\"enableVoiceMode\"] = True\n\n            if conversation_id is None:\n                url = f\"{cls.conversation_url}/new\"\n            else:\n                url = f\"{cls.conversation_url}/{conversation_id}/responses\"\n\n            async with session.post(\n                url, json=payload, headers={\"x-xai-request-id\": str(uuid.uuid4())}\n            ) as response:\n                if response.status == 403:\n                    raise MissingAuthError(\"Invalid secrets\")\n                auth_result.cookies = merge_cookies(auth_result.cookies, response)\n                await raise_for_status(response)\n\n                thinking_duration = None\n                deep_search_active = False\n\n                async for line in response.iter_lines():\n                    if line:\n                        try:\n                            json_data = json.loads(line)\n                            result = json_data.get(\"result\", {})\n\n                            if conversation_id is None:\n                                conversation_id = result.get(\"conversation\", {}).get(\n                                    \"conversationId\"\n                                )\n\n                            response_data = result.get(\"response\", {})","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Grok.py#L196-L232","documentation":"Grok's conversation endpoint returned HTTP 403, which the provider interprets as authentication failure and raises as MissingAuthError('Invalid secrets'). Grok here is driven by browser-derived cookies (auth_result.cookies), so 403 means those cookies or tokens are expired, revoked, or never valid for grok.com.","triggerScenarios":"POST to the /responses conversation endpoint with stale sso/sso-rw cookies; Cloudflare challenge blocking the API path; account logged out in the browser so cookies no longer authorize API calls.","commonSituations":"Cookies harvested days earlier and since rotated; using grok.com cookies for an account without access; Cloudflare bot detection triggered by datacenter IPs; g4f version outdated relative to Grok's endpoint changes.","solutions":["Re-harvest fresh grok.com cookies (log in, export cookies, pass them to the provider or update the cookie jar) and retry","Catch MissingAuthError and route re-auth automatically before retrying once","Use a residential or clean egress IP if Cloudflare is the trigger (403 with challenge HTML body)","Update g4f; Grok's internal endpoints and required headers change often"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"def grok_cookies_present(cookies: dict) -> bool:\n    return bool(cookies.get(\"sso\") and cookies.get(\"sso-rw\"))\n\nif not grok_cookies_present(cookies):\n    raise ConfigurationError(\"refresh grok.com cookies before using this provider\")","typeGuard":null,"tryCatchPattern":"from g4f.errors import MissingAuthError\ntry:\n    async for chunk in Grok.create_async_generator(model, messages):\n        ...\nexcept MissingAuthError as e:\n    if \"Invalid secrets\" in str(e):\n        await refresh_grok_cookies()   # re-harvest from a logged-in browser\n        async for chunk in Grok.create_async_generator(model, messages):\n            ...","preventionTips":["Re-harvest grok.com cookies on a schedule; they rotate frequently","Catch MissingAuthError separately and re-auth; do not blind-retry","Prefer clean egress IPs to avoid Cloudflare 403s being misread as bad cookies"],"tags":["auth","grok","cookies","http-403","cloudflare"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}