router-for-me/CLIProxyAPI · error

kimi: failed to create token request: %w

Error message

kimi: failed to create token request: %w

What it means

http.NewRequestWithContext failed while building the token-exchange POST to https://auth.kimi.com/api/oauth/token. In practice this only happens for a malformed URL or an invalid method/nil body — all inputs here are hardcoded constants, so a failure is essentially a programming or environment corruption problem, not a network one.

Source

Thrown at internal/auth/kimi/kimi.go:275

			if !shouldContinue {
				return nil, pollErr
			}
			// Continue polling
		}
	}
}

// exchangeDeviceCode attempts to exchange the device code for an access token.
// Returns (token, error, shouldContinue).
func (c *DeviceFlowClient) exchangeDeviceCode(ctx context.Context, deviceCode string) (*KimiTokenData, error, bool) {
	data := url.Values{}
	data.Set("client_id", kimiClientID)
	data.Set("device_code", deviceCode)
	data.Set("grant_type", "urn:ietf:params:oauth:grant-type:device_code")

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, kimiTokenURL, strings.NewReader(data.Encode()))
	if err != nil {
		return nil, fmt.Errorf("kimi: failed to create token request: %w", err), false
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Accept", "application/json")
	for k, v := range c.commonHeaders() {
		req.Header.Set(k, v)
	}

	resp, err := c.httpClient.Do(req)
	if err != nil {
		return nil, fmt.Errorf("kimi: token request failed: %w", err), false
	}
	defer func() {
		if errClose := resp.Body.Close(); errClose != nil {
			log.Errorf("kimi token exchange: close body error: %v", errClose)
		}
	}()

	bodyBytes, err := io.ReadAll(resp.Body)

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. If you forked/patched the file, verify kimiTokenURL is a valid absolute URL (https://auth.kimi.com/api/oauth/token)
  2. Otherwise treat as a bug: report it — the stock binary should never hit this path
Defensive patterns

Strategy: validation

Validate before calling

// Only relevant for forks: assert the constant parses
if _, err := url.Parse(kimiTokenURL); err != nil {
    return nil, fmt.Errorf("bad kimiTokenURL: %w", err)
}

Prevention

When it happens

Trigger: Passing a nil context (NewRequestWithContext panics on nil ctx instead — so realistically only URL parse failure), or the constants kimiTokenURL/method somehow altered. Not reachable in a correctly built binary.

Common situations: Custom forks that change kimiTokenURL to an invalid value (typo, missing scheme), unit tests stubbing the constructor incorrectly.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/4f4bd1128b77dc71. Report an issue: GitHub.