{"record":{"id":"86df5c106d738b07","repo":"AlexxIT/go2rtc","slug":"authentication-failed-while-creating-session-w","errorCode":null,"errorMessage":"authentication failed while creating session: %w","messagePattern":"authentication failed while creating session: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/ring/api.go","lineNumber":523,"sourceCode":"\n\t\tbreak\n\t}\n\n\treturn responseBody, nil\n}\n\nfunc (c *RingApi) ensureSession() error {\n\tc.sessionMutex.Lock()\n\tdefer c.sessionMutex.Unlock()\n\n\t// If session is still valid, use it\n\tif c.session != nil && time.Now().Before(c.sessionExpiry) {\n\t\treturn nil\n\t}\n\n\t// Make sure we have a valid auth token\n\tif err := c.ensureAuth(); err != nil {\n\t\treturn fmt.Errorf(\"authentication failed while creating session: %w\", err)\n\t}\n\n\tsessionPayload := map[string]interface{}{\n\t\t\"device\": map[string]interface{}{\n\t\t\t\"hardware_id\": c.hardwareID,\n\t\t\t\"metadata\": map[string]interface{}{\n\t\t\t\t\"api_version\":  apiVersion,\n\t\t\t\t\"device_model\": \"ring-client-go\",\n\t\t\t},\n\t\t\t\"os\": \"android\",\n\t\t},\n\t}\n\n\tbody, err := json.Marshal(sessionPayload)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal session request: %w\", err)\n\t}\n","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/ring/api.go#L505-L541","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check the wrapped cause (%w) to see whether it is 2FA required, bad credentials, or a network error and address that first","Re-authenticate with fresh credentials / a valid refresh token in the AuthConfig","If 2FA is enabled on the account, supply the 2FA code per the library's documented 2FA flow","Verify network connectivity and that the Ring OAuth endpoint is reachable"],"exampleFix":"// before\nc := ring.NewClient(cfg) // stale refresh token\nsessions, err := c.ActiveDings()\n// after\nif err := c.RefreshAuthToken(context.Background()); err != nil {\n    log.Fatalf(\"re-authenticate: %v\", err) // fix credentials/2FA before retrying\n}","handlingStrategy":"try-catch","validationCode":"if client.IsAuthConfigured(cfg) { /* creds or refresh token present */ }","typeGuard":"func isAuthError(err error) bool { return err != nil && strings.Contains(err.Error(), \"authentication failed\") }","tryCatchPattern":"if err := client.SomeCall(ctx); err != nil {\n    var authErr *ring.AuthError\n    if errors.As(err, &authErr) || strings.Contains(err.Error(), \"authentication failed\") {\n        // re-authenticate / fix credentials before retry\n    }\n    return err\n}","preventionTips":["Persist and reuse the refresh token instead of storing only passwords","Run an explicit auth/refresh check at startup before making API calls","Handle 2FA proactively if the account has it enabled","Refresh the session before its expiry rather than waiting for lazy failure"],"tags":["authentication","session","ring","wrapped-error"],"backgroundTag":"authentication-required","analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}