{"record":{"id":"858d0bc9189916cf","repo":"xtekky/gpt4free","slug":"tokenerror-file-access-error","errorCode":"TokenError.FILE_ACCESS_ERROR","errorMessage":"Credentials file not found","messagePattern":"Credentials file not found","errorType":"exception","errorClass":"TokenManagerError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/github/sharedTokenManager.py","lineNumber":100,"sourceCode":"                self.reloadCredentialsFromFile()\n                self.memory_cache[\"file_mod_time\"] = file_mod_time\n        except FileNotFoundError:\n            self.memory_cache[\"file_mod_time\"] = 0\n        except Exception as e:\n            self.memory_cache[\"credentials\"] = None\n            raise TokenManagerError(TokenError.FILE_ACCESS_ERROR, str(e), e)\n\n    def reloadCredentialsFromFile(self):\n        file_path = self.getCredentialFilePath()\n        debug.log(f\"Reloading credentials from {file_path}\")\n        try:\n            with open(file_path, \"r\") as fs:\n                data = json.load(fs)\n                credentials = self.validateCredentials(data)\n                self.memory_cache[\"credentials\"] = credentials\n        except FileNotFoundError as e:\n            self.memory_cache[\"credentials\"] = None\n            raise TokenManagerError(\n                TokenError.FILE_ACCESS_ERROR, \"Credentials file not found\", e\n            ) from e\n        except json.JSONDecodeError as e:\n            self.memory_cache[\"credentials\"] = None\n            raise TokenManagerError(\n                TokenError.FILE_ACCESS_ERROR, \"Invalid JSON format\", e\n            ) from e\n        except Exception as e:\n            self.memory_cache[\"credentials\"] = None\n            raise TokenManagerError(TokenError.FILE_ACCESS_ERROR, str(e), e) from e\n\n    def validateCredentials(self, data):\n        if not data or not isinstance(data, dict):\n            raise ValueError(\"Invalid credentials format\")\n        if \"access_token\" not in data or not isinstance(data[\"access_token\"], str):\n            raise ValueError(\"Invalid credentials: missing access_token\")\n        if \"token_type\" not in data or not isinstance(data[\"token_type\"], str):\n            raise ValueError(\"Invalid credentials: missing token_type\")","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/github/sharedTokenManager.py#L82-L118","documentation":"TokenManagerError (code FILE_ACCESS_ERROR) raised in SharedTokenManager.reloadCredentialsFromFile when opening the credential file raises FileNotFoundError. The manager clears its in-memory cache and re-raises wrapped, so any caller that assumed persisted credentials exist fails loudly instead of silently using stale tokens.","triggerScenarios":"Any credentials read/refresh attempt (getValidCredentials, CopilotTokenProvider token exchange) when the credential JSON file does not exist at getCredentialFilePath() — typically because login never ran in this environment.","commonSituations":"Fresh installs; containers/CI where HOME differs from the machine where 'g4f auth' was run; credential directory wiped by cleanup tooling.","solutions":["Run 'g4f auth github-copilot' in the same environment/user that runs the app to create the file","Verify getCredentialFilePath() resolves to an existing file in the runtime (check HOME/XDG_CONFIG_HOME in Docker, systemd, cron)","Pre-create/mount the credential directory if deploying to read-only or ephemeral filesystems"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ncred_path = Path(sm.getCredentialFilePath())\nif not cred_path.is_file():\n    raise SystemExit('Run g4f auth github-copilot first')","typeGuard":null,"tryCatchPattern":"from g4f.Provider.github.sharedTokenManager import TokenManagerError\n\ntry:\n    creds = await sm.getValidCredentials(client)\nexcept TokenManagerError as e:\n    if 'Credentials file not found' in str(e):\n        creds = await do_device_login()","preventionTips":["Run 'g4f auth' in the same container/user as the app; bake or mount the credential file in Docker images","Assert the credential file exists at boot","Never assume a prior machine's login carries over"],"tags":["credentials","file-io","token-manager","configuration"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}