{"record":{"id":"363753f3341cb703","repo":"router-for-me/CLIProxyAPI","slug":"xai-device-token-create-request-w","errorCode":null,"errorMessage":"xai device token: create request: %w","messagePattern":"xai device token: create request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/auth/xai/xai.go","lineNumber":266,"sourceCode":"\t\t\t}\n\t\t\tinterval = nextInterval\n\t\t\ttimer.Reset(interval)\n\t\t}\n\t}\n}\n\n// exchangeDeviceCode attempts to exchange a device code for tokens.\n// Returns (token, error, nextInterval, shouldContinue).\nfunc (a *XAIAuth) exchangeDeviceCode(ctx context.Context, tokenEndpoint, deviceCode string, interval time.Duration) (*TokenData, error, time.Duration, bool) {\n\tform := url.Values{\n\t\t\"grant_type\":  {DeviceCodeGrantType},\n\t\t\"device_code\": {strings.TrimSpace(deviceCode)},\n\t\t\"client_id\":   {ClientID},\n\t}\n\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSpace(tokenEndpoint), strings.NewReader(form.Encode()))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token: create request: %w\", err), interval, false\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/x-www-form-urlencoded\")\n\treq.Header.Set(\"Accept\", \"application/json\")\n\n\tresp, err := a.httpClient.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token request failed: %w\", err), interval, false\n\t}\n\tdefer func() {\n\t\tif errClose := resp.Body.Close(); errClose != nil {\n\t\t\tlog.Errorf(\"xai device token: close response body error: %v\", errClose)\n\t\t}\n\t}()\n\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token: read response: %w\", err), interval, false\n\t}","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/auth/xai/xai.go#L248-L284","documentation":"http.NewRequestWithContext failed while building the token-exchange POST. This is a local, pre-network failure: the tokenEndpoint URL could not be parsed into a valid request, so the request object could not even be created.","triggerScenarios":"tokenEndpoint is empty, not a URL, or malformed after TrimSpace — e.g. discovery returned an unusable TokenEndpoint, or a hand-crafted endpoint string with bad scheme/control characters.","commonSituations":"Discovery document changed shape or was proxied and returned a relative/HTML value for token_endpoint; manual configuration override of the token endpoint with a typo; trailing newline or space characters in a stored endpoint.","solutions":["Log/inspect the tokenEndpoint string passed to exchangeDeviceCode","Re-run discovery and check the token_endpoint value it returns","Validate the URL (url.Parse + scheme check) before starting the device flow"],"exampleFix":"// before\ntokenData, err := auth.WaitForAuthorization(ctx, deviceCode)\n\n// after\nif _, uerr := url.Parse(strings.TrimSpace(deviceCode.TokenEndpoint)); uerr != nil {\n    return fmt.Errorf(\"invalid token endpoint %q: %w\", deviceCode.TokenEndpoint, uerr)\n}\ntokenData, err := auth.WaitForAuthorization(ctx, deviceCode)","handlingStrategy":"validation","validationCode":"if u, err := url.Parse(strings.TrimSpace(deviceCode.TokenEndpoint)); err != nil || u.Scheme == \"\" || u.Host == \"\" {\n    return fmt.Errorf(\"invalid xAI token endpoint: %q\", deviceCode.TokenEndpoint)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate endpoint URLs with url.Parse and scheme/host checks before starting the flow","Trust endpoints only from a successful Discover call, not hand-edited config"],"tags":["xai","auth","url","request-build"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}