{"record":{"id":"c8adce079f382b92","repo":"BerriAI/litellm","slug":"authentication-failed-check-your-gitlab-token-and","errorCode":null,"errorMessage":"Authentication failed. Check your GitLab token and auth_method.","messagePattern":"Authentication failed\\. Check your GitLab token and auth_method\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/integrations/gitlab/gitlab_client.py","lineNumber":149,"sourceCode":"\n            ctype: Final = (resp.headers.get(\"content-type\") or \"\").lower()\n            if ctype.startswith(\"text/\") or \"charset=\" in ctype or ctype.startswith(\"application/json\"):\n                return resp.text\n            try:\n                return resp.content.decode(\"utf-8\")\n            except Exception:\n                return resp.content.decode(\"utf-8\", errors=\"replace\")\n\n        except Exception as e:\n            status: Final = getattr(getattr(e, \"response\", None), \"status_code\", None)\n            if status == 404:\n                return None\n            if status == 403:\n                raise Exception(\n                    f\"Access denied to file '{file_path}'. Check your GitLab permissions for project '{self.project}'.\"\n                )\n            if status == 401:\n                raise Exception(\"Authentication failed. Check your GitLab token and auth_method.\")\n            raise Exception(f\"Failed to fetch file '{file_path}': {e}\")\n\n    def _get_file_content_via_json(self, file_path: str, *, ref: str | None = None) -> str | None:\n        \"\"\"\n        Fallback for get_file_content(): use the JSON file API which returns base64 content.\n        \"\"\"\n        json_url: Final = self._file_json_url(file_path, ref=ref)\n        try:\n            resp: Final = self.http_handler.get(json_url, headers=self.headers)\n            if resp.status_code == 404:\n                return None\n            resp.raise_for_status()\n            data: Final = resp.json()\n            content: Final = data.get(\"content\")\n            encoding: Final = data.get(\"encoding\", \"\")\n            if content and encoding == \"base64\":\n                try:\n                    return base64.b64decode(content).decode(\"utf-8\")","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/integrations/gitlab/gitlab_client.py#L131-L167","documentation":"GitLab answered 401 Unauthorized for the raw file endpoint: the token is missing, expired, revoked, or sent under the wrong scheme. GitLabClient sends the token as a Private-Token header by default and as Authorization: Bearer only when config['auth_method'] == 'oauth', so an OAuth token left on the default auth_method is mis-sent and rejected.","triggerScenarios":"Expired or revoked PAT; token copied with whitespace or a typo; OAuth token used while auth_method stays 'token'; CI job token used against a different instance; token issued for a different base_url (self-hosted vs gitlab.com).","commonSituations":"Rotated tokens not propagated to every service; stale env vars in CI runners; migrating from PAT to OAuth without flipping auth_method; mixing tokens between a self-hosted instance and gitlab.com.","solutions":["Regenerate/verify the token and update the config or env var everywhere","Set auth_method='oauth' when the credential is an OAuth/bearer token","Confirm base_url matches the GitLab instance the token belongs to","Call client.test_connection() at startup to fail fast"],"exampleFix":"# before: OAuth token sent via the default Private-Token header -> 401\ngitlab_config = {\"project\": \"mygroup/myrepo\", \"access_token\": oauth_token}\n\n# after\ngitlab_config = {\n    \"project\": \"mygroup/myrepo\",\n    \"access_token\": oauth_token,\n    \"auth_method\": \"oauth\",\n}","handlingStrategy":"validation","validationCode":"# Preflight credentials before any prompt fetch\nif not client.test_connection():\n    raise RuntimeError(\n        \"GitLab authentication failed - check access_token, \"\n        \"auth_method (token vs oauth), and base_url\"\n    )","typeGuard":"from typing import Any\n\n\ndef looks_like_oauth_token(token: Any) -> bool:\n    \"\"\"Heuristic: OAuth/job tokens usually carry a dot; PATs do not.\"\"\"\n    t = str(token or \"\")\n    return \".\" in t and not t.startswith(\"glpat-\")","tryCatchPattern":"try:\n    content = client.get_file_content(\"prompts/greet.prompt\")\nexcept Exception as e:\n    if \"Authentication failed\" in str(e):\n        raise RuntimeError(\n            \"GitLab token rejected - rotate it and verify auth_method\"\n        ) from e\n    raise","preventionTips":["Run test_connection() during startup for every GitLab-backed integration","Automate token rotation so expiries do not surface as runtime 401s","Pair OAuth tokens with auth_method='oauth' in one shared config helper"],"tags":["gitlab","auth","http-401","unauthorized","token"],"backgroundTag":"http-401-unauthorized","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}