goharbor/harbor · error

unmarshal token response error

Error message

unmarshal token response error: %v

What it means

refreshToken got a 2xx login response but json.Unmarshal of the body into TokenResp failed — the token payload schema differs from expected or the body is not the anticipated JSON; the parse error is wrapped.

Solutions

  1. Log the response body to inspect the actual token payload
  2. Check for Docker Hub API schema changes
  3. Update TokenResp to match the current token response format
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/pkg/reg/adapter/dockerhub/client.go:106 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of goharbor/harbor@7b2fd08cc5 (2026-08-16). Data as JSON: /api/errors/ab3dd336db0dad14. Report an issue: GitHub.

Appendix: source

Thrown at src/pkg/reg/adapter/dockerhub/client.go:106

	resp, err := c.client.Do(request)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	if resp.StatusCode/100 != 2 {
		return fmt.Errorf("login to dockerhub error: %s", string(body))
	}

	token := &TokenResp{}
	if err = json.Unmarshal(body, token); err != nil {
		return fmt.Errorf("unmarshal token response error: %v", err)
	}

	c.token = token.AccessToken
	// Tokens issued by /v2/auth/token expire after 10 minutes; refresh 1 minute
	// early to avoid using a token that is about to expire.
	c.tokenExpiry = time.Now().Add(9 * time.Minute)
	return nil
}

// ensureToken refreshes the bearer token when it has expired or is close to
// expiring. It is a no-op for anonymous (unauthenticated) clients.
func (c *Client) ensureToken() error {
	if len(c.credential.Identifier) == 0 {
		return nil
	}
	c.mu.Lock()
	defer c.mu.Unlock()
	if time.Now().Before(c.tokenExpiry) {

View on GitHub (pinned to 7b2fd08cc5)