AlexxIT/go2rtc · critical

${tokenResp.Msg}

Error message

${tokenResp.Msg}

What it means

initToken returns this error when the Tuya login-token API responds with Success=false; tokenResp.Msg is passed to errors.New directly. The token bootstrap step failed, so no session can be established and all subsequent API calls will also fail. The server's message is surfaced verbatim.

Solutions

  1. Read Msg: authentication failures require credential fixes; server errors allow a retry.
  2. Verify username/password and region configuration used to construct TuyaSmartApiClient.
  3. Re-run the full login flow after fixing credentials.
  4. Add bounded retry with backoff for transient cloud errors only.
  5. Check for captcha/2FA requirements indicated in the message.

Example fix

// before
c := tuya.NewClient(user, pass, region)
if err := c.initToken(); err != nil {
    return err
}
// after
if err := c.initToken(); err != nil {
    return fmt.Errorf("tuya token init for %s@%s: %w", user, region, err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if username == "" || password == "" || region == "" { return errors.New("missing tuya credentials/region") } // before initToken

Try / catch

if err := c.initToken(); err != nil {
    if strings.Contains(err.Error(), "password") || strings.Contains(err.Error(), "account") {
        return ErrBadCredentials // do not retry
    }
    return fmt.Errorf("init token: %w", err) // transient: retry with backoff
}

Prevention

When it happens

Trigger: Calling initToken (or any higher-level call that triggers it) when the login-token endpoint returns Success=false — e.g. wrong username/password, disabled account, or cloud rejection.

Common situations: Changed Tuya account password; account locked or captcha required; wrong region endpoint; network path to a stale endpoint; Tuya SDK version mismatch.

Related errors


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

Appendix: source

Thrown at pkg/tuya/smart_api.go:432

	tokenReq := LoginTokenRequest{
		CountryCode: c.countryCode,
		Username:    c.email,
		IsUid:       false,
	}

	body, err := c.request("POST", tokenUrl, tokenReq)
	if err != nil {
		return err
	}

	var tokenResp LoginTokenResponse
	if err := json.Unmarshal(body, &tokenResp); err != nil {
		return err
	}

	if !tokenResp.Success {
		return errors.New(tokenResp.Msg)
	}

	encryptedPassword, err := EncryptPassword(c.password, tokenResp.Result.PbKey)
	if err != nil {
		return fmt.Errorf("failed to encrypt password: %v", err)
	}
	var loginUrl string

	loginReq := PasswordLoginRequest{
		CountryCode: c.countryCode,
		Passwd:      encryptedPassword,
		Token:       tokenResp.Result.Token,
		IfEncrypt:   1,
		Options:     `{"group":1}`,
	}

	if IsEmailAddress(c.email) {
		loginUrl = fmt.Sprintf("https://%s/api/private/email/login", c.baseUrl)

View on GitHub (pinned to c245815e75)