{"record":{"id":"fdc1513b5e43712d","repo":"iflytek/astron-agent","slug":"invalid-host-url-or-authentication-parameters","errorCode":null,"errorMessage":"Invalid host URL or authentication parameters","messagePattern":"Invalid host URL or authentication parameters","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotAIServiceClient.java","lineNumber":553,"sourceCode":"            mac.init(spec);\n\n            byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));\n            String sha = Base64.getEncoder().encodeToString(hexDigits);\n\n            String authorization = String.format(\"api_key=\\\"%s\\\", algorithm=\\\"%s\\\", headers=\\\"%s\\\", signature=\\\"%s\\\"\",\n                    apiKey, \"hmac-sha256\", \"host date request-line\", sha);\n\n            HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse(\"https://\" + uri.getHost() + uri.getPath()))\n                    .newBuilder()\n                    .addQueryParameter(\"authorization\", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8)))\n                    .addQueryParameter(\"date\", date)\n                    .addQueryParameter(\"host\", uri.getHost())\n                    .build();\n\n            return httpUrl.toString();\n        } catch (Exception e) {\n            log.error(\"Failed to build WebSocket authentication URL\", e);\n            throw new IllegalArgumentException(\"Invalid host URL or authentication parameters\", e);\n        }\n    }\n\n    /**\n     * WebSocket response data structure\n     */\n    @Data\n    @JsonIgnoreProperties(ignoreUnknown = true)\n    public static class WebSocketResponse {\n        private ResponseHeader header;\n        private ResponsePayload payload;\n    }\n\n    @Data\n    @JsonIgnoreProperties(ignoreUnknown = true)\n    public static class ResponseHeader {\n        private int code;\n        private String sid;","sourceCodeStart":535,"sourceCodeEnd":571,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotAIServiceClient.java#L535-L571","documentation":"buildWebSocketAuthUrl signs the Spark chat WebSocket endpoint (HMAC-SHA256 of host/date/request-line) and returns the authenticated URL. Any failure — URI.create on hostUrl, HttpUrl.parse returning null (OkHttp cannot parse the composed URL), or MAC operations — is logged and rethrown as IllegalArgumentException('Invalid host URL or authentication parameters', cause). Thrown before any WebSocket connection is attempted.","triggerScenarios":"TEXT_HOST_URL is malformed or its host+path cannot be parsed by OkHttp's HttpUrl (Objects.requireNonNull fails), apiSecret is null, or URI.create rejects the host URL string when generateText calls buildWebSocketAuthUrl(TEXT_HOST_URL, ...).","commonSituations":"TEXT_HOST_URL constant edited to an invalid value (bad path/host), platform API secret missing from config causing NPE, or URL characters needing encoding in the configured host URL.","solutions":["Check the logged 'Failed to build WebSocket authentication URL' cause for the exact failure","Verify TEXT_HOST_URL ('https://spark-api.xf-yun.com/v4.0/chat') is intact and parses as an absolute URL with host and path","Ensure platformApiKey/platformApiSecret are configured and non-null for the iFlytek open platform account","Unit-test buildWebSocketAuthUrl with the configured URL to catch regressions after config changes"],"exampleFix":"// before\nString authUrl = buildWebSocketAuthUrl(TEXT_HOST_URL, config.getPlatformApiKey(), config.getPlatformApiSecret());\n// after\nif (StrUtil.isBlank(config.getPlatformApiKey()) || StrUtil.isBlank(config.getPlatformApiSecret())) {\n    throw new BusinessException(ResponseEnum.CONFIG_ERROR);\n}\nString authUrl = buildWebSocketAuthUrl(TEXT_HOST_URL, config.getPlatformApiKey(), config.getPlatformApiSecret());","handlingStrategy":"validation","validationCode":"if (StrUtil.isBlank(apiKey) || StrUtil.isBlank(apiSecret)) { throw new BusinessException(ResponseEnum.CONFIG_ERROR); }\nassert HttpUrl.parse(\"https://spark-api.xf-yun.com/v4.0/chat\") != null;","typeGuard":null,"tryCatchPattern":"try { return client.generateText(q, domain, 60); } catch (IllegalArgumentException e) { log.error(\"WS auth URL invalid — check host URL and credentials\", e); throw new BusinessException(ResponseEnum.CONFIG_ERROR); }","preventionTips":["Do not edit TEXT_HOST_URL without validating it parses as host+path","Configure apiKey/apiSecret for the iFlytek platform account","Add a unit test covering buildWebSocketAuthUrl with the production URL"],"tags":["java","websocket","hmac","url-building"],"backgroundTag":"invalid-url-format","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}