{"record":{"id":"71701935a7de5d13","repo":"microsoft/semantic-kernel","slug":"failed-to-fetch-token-from-token-endpoint","errorCode":null,"errorMessage":"Failed to fetch token from token endpoint.","messagePattern":"Failed to fetch token from token endpoint\\.","errorType":"exception","errorClass":"AgentInvokeException","httpStatus":null,"severity":"error","filePath":"python/samples/demos/copilot_studio_agent/src/direct_line_agent.py","lineNumber":78,"sourceCode":"                        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(\n        self,\n        history: ChatHistory,\n        arguments: dict[str, Any] | None = None,\n        **kwargs: Any,\n    ) -> ChatMessageContent:\n        \"\"\"\n        Get a response from the DirectLine Bot.\n        \"\"\"\n        responses = []\n        async for response in self.invoke(history, arguments, **kwargs):\n            responses.append(response)","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/samples/demos/copilot_studio_agent/src/direct_line_agent.py#L60-L96","documentation":"An AgentInvokeException raised in the no-bot_secret branch when GET token_endpoint returns a non-200 status. The endpoint rejected the request outright; the status is logged.","triggerScenarios":"GET self.token_endpoint returns 4xx/5xx (404 wrong URL, 401 needs auth, 5xx server fault).","commonSituations":"token_endpoint misconfigured or stale; the token service requires auth that wasn't supplied; service outage.","solutions":["Correct the TOKEN_ENDPOINT value to the real token-issuing URL.","Use the bot_secret flow if the token_endpoint requires authentication.","Retry on transient 5xx; treat 4xx as configuration errors."],"exampleFix":"// before\nTOKEN_ENDPOINT=https://example.com/api\n\n// after\nTOKEN_ENDPOINT=https://directline.botframework.com/v3/directline/tokens/generate","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await fetch_token()\nexcept AgentInvokeException as e:\n    if \"Failed to fetch token from token endpoint\" in str(e):\n        # 5xx is transient; 4xx is config\n        logger.warning(\"Token endpoint unreachable, will retry once\")\n        await asyncio.sleep(2); await fetch_token()\n    raise","preventionTips":["Validate the token_endpoint URL resolves (curl/DNS) before running.","Add request timeouts to distinguish slow endpoints from outages."],"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"}