AlexxIT/go2rtc · error

authentication failed while creating session

Error message

authentication failed while creating session: %w

What it means

This error wraps any failure from ensureAuth() while the Ring client is trying to create a new API session. The library checks for a valid cached session first; if none exists or it expired, it must obtain a fresh auth token before POSTing to the session endpoint. If token acquisition (credentials, refresh token, 2FA) fails, the session cannot be created and this wrapped error is returned.

Solutions

  1. Check the wrapped cause (%w) to see whether it is 2FA required, bad credentials, or a network error and address that first
  2. Re-authenticate with fresh credentials / a valid refresh token in the AuthConfig
  3. If 2FA is enabled on the account, supply the 2FA code per the library's documented 2FA flow
  4. Verify network connectivity and that the Ring OAuth endpoint is reachable

Example fix

// before
c := ring.NewClient(cfg) // stale refresh token
sessions, err := c.ActiveDings()
// after
if err := c.RefreshAuthToken(context.Background()); err != nil {
    log.Fatalf("re-authenticate: %v", err) // fix credentials/2FA before retrying
}
Defensive patterns

Strategy: try-catch

Validate before calling

if client.IsAuthConfigured(cfg) { /* creds or refresh token present */ }

Type guard

func isAuthError(err error) bool { return err != nil && strings.Contains(err.Error(), "authentication failed") }

Try / catch

if err := client.SomeCall(ctx); err != nil {
    var authErr *ring.AuthError
    if errors.As(err, &authErr) || strings.Contains(err.Error(), "authentication failed") {
        // re-authenticate / fix credentials before retry
    }
    return err
}

Prevention

When it happens

Trigger: Calling any API method that requires a session when c.session is nil or time.Now() is past c.sessionExpiry, and ensureAuth() then fails (bad credentials, expired/invalid refresh token, 2FA required, network error, non-200 auth response).

Common situations: Expired refresh token after long idle; wrong email/password in AuthConfig; Ring account with 2FA enabled but no 2FA handling; Ring rotating client secrets; network/proxy blocking oauth endpoint.

Understand the failure class

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/86df5c106d738b07. Report an issue: GitHub.

Appendix: source

Thrown at pkg/ring/api.go:523

		break
	}

	return responseBody, nil
}

func (c *RingApi) ensureSession() error {
	c.sessionMutex.Lock()
	defer c.sessionMutex.Unlock()

	// If session is still valid, use it
	if c.session != nil && time.Now().Before(c.sessionExpiry) {
		return nil
	}

	// Make sure we have a valid auth token
	if err := c.ensureAuth(); err != nil {
		return fmt.Errorf("authentication failed while creating session: %w", err)
	}

	sessionPayload := map[string]interface{}{
		"device": map[string]interface{}{
			"hardware_id": c.hardwareID,
			"metadata": map[string]interface{}{
				"api_version":  apiVersion,
				"device_model": "ring-client-go",
			},
			"os": "android",
		},
	}

	body, err := json.Marshal(sessionPayload)
	if err != nil {
		return fmt.Errorf("failed to marshal session request: %w", err)
	}

View on GitHub (pinned to c245815e75)