{"record":{"id":"d1bf6de9dc490a5d","repo":"microsoft/semantic-kernel","slug":"failed-to-generate-token-using-bot-secret","errorCode":null,"errorMessage":"Failed to generate token using bot_secret.","messagePattern":"Failed to generate token using bot_secret\\.","errorType":"exception","errorClass":"AgentInvokeException","httpStatus":null,"severity":"error","filePath":"python/samples/demos/copilot_studio_agent/src/direct_line_agent.py","lineNumber":67,"sourceCode":"        \"\"\"\n        Retrieve the DirectLine token either by using the bot_secret or by querying the token_endpoint.\n        If bot_secret is provided, it posts to \"https://directline.botframework.com/v3/directline/tokens/generate\".\n        \"\"\"\n        await self._ensure_session()\n        try:\n            if self.bot_secret:\n                url = f\"{self.bot_endpoint}/tokens/generate\"\n                headers = {\"Authorization\": f\"Bearer {self.bot_secret}\"}\n                async with self.session.post(url, headers=headers) as resp:\n                    if resp.status == 200:\n                        data = await resp.json()\n                        self.directline_token = data.get(\"token\")\n                        if not self.directline_token:\n                            logger.error(\"Token generation response missing token: %s\", data)\n                            raise AgentInvokeException(\"No token received from token generation.\")\n                    else:\n                        logger.error(\"Token generation endpoint error status: %s\", resp.status)\n                        raise AgentInvokeException(\"Failed to generate token using bot_secret.\")\n            else:\n                async with self.session.get(self.token_endpoint) as resp:\n                    if resp.status == 200:\n                        data = await resp.json()\n                        self.directline_token = data.get(\"token\")\n                        if not self.directline_token:\n                            logger.error(\"Token endpoint returned no token: %s\", data)\n                            raise AgentInvokeException(\"No token received.\")\n                    else:\n                        logger.error(\"Token endpoint error status: %s\", resp.status)\n                        raise AgentInvokeException(\"Failed to fetch token from token endpoint.\")\n        except Exception as ex:\n            logger.exception(\"Exception fetching token: %s\", ex)\n            raise AgentInvokeException(\"Exception occurred while fetching token.\") from ex\n\n    @trace_agent_get_response\n    @override\n    async def get_response(","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/copilot_studio_agent/src/direct_line_agent.py#L49-L85","documentation":"An AgentInvokeException raised when the bot_secret token-generation POST returns a non-200 HTTP status. This means the request itself was rejected (auth failure, bad endpoint, server error) before any token could be parsed; the status code is logged.","triggerScenarios":"POST to {bot_endpoint}/tokens/generate with Bearer {bot_secret} returns a status outside the 2xx range (401/403 for bad secret, 404 for wrong endpoint, 5xx for server fault).","commonSituations":"Expired or incorrect bot_secret; bot_endpoint misconfigured; DirectLine/Copilot service outage; network proxy returning an error page.","solutions":["Regenerate the DirectLine/Copilot secret and set the correct BOT_SECRET value.","Verify bot_endpoint is the full base URL of your DirectLine relay (no trailing path mismatch).","Check the logged status code: 401/403 => secret problem; 404 => endpoint; 5xx => upstream outage worth retrying."],"exampleFix":"// before\nBOT_SECRET=old-or-wrong-secret\n\n// after\nBOT_SECRET=<regenerated secret from Copilot Studio DirectLine channel>","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        await fetch_token()\n        break\n    except AgentInvokeException as e:\n        if \"Failed to generate token\" in str(e) and attempt < 2:\n            await asyncio.sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Store the DirectLine secret in a secret manager, not hard-coded, so rotation is easy.","Verify the endpoint returns 200 with a local curl before wiring the agent."],"tags":["network","authentication","directline","copilot-studio","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}