{"record":{"id":"40444d311f070b39","repo":"AlexxIT/go2rtc","slug":"failed-to-initialize-token-w-40444d","errorCode":null,"errorMessage":"failed to initialize token: %w","messagePattern":"failed to initialize token: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/tuya/smart_api.go","lineNumber":300,"sourceCode":"\t\tTuyaClient: TuyaClient{\n\t\t\thttpClient: httpClient,\n\t\t\tmqtt:       mqttClient,\n\t\t\tdeviceId:   deviceId,\n\t\t\texpireTime: 0,\n\t\t\tbaseUrl:    baseUrl,\n\t\t},\n\t\temail:       email,\n\t\tpassword:    password,\n\t\tcountryCode: region.Continent,\n\t}\n\n\treturn client, nil\n}\n\n// WebRTC Flow\nfunc (c *TuyaSmartApiClient) Init() error {\n\tif err := c.initToken(); err != nil {\n\t\treturn fmt.Errorf(\"failed to initialize token: %w\", err)\n\t}\n\n\twebrtcConfig, err := c.loadWebrtcConfig()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to load webrtc config: %w\", err)\n\t}\n\n\thubConfig, err := c.loadHubConfig()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to load hub config: %w\", err)\n\t}\n\n\tif err := c.mqtt.Start(hubConfig, webrtcConfig, c.skill.WebRTC); err != nil {\n\t\treturn fmt.Errorf(\"failed to start MQTT: %w\", err)\n\t}\n\n\tif c.skill.LowPower > 0 {\n\t\t_ = c.mqtt.WakeUp(c.localKey)","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/tuya/smart_api.go#L282-L318","documentation":"TuyaSmartApiClient.Init begins by calling initToken to obtain/refresh the cloud access token; any failure there is wrapped as 'failed to initialize token'. This is the entry-point failure of the WebRTC flow — without a valid token nothing else (webrtc config, hub config) can load. The wrapped inner error carries the actual HTTP/sign/JSON cause.","triggerScenarios":"Init() when initToken fails: wrong accessId/accessSecret, unreachable base URL, HTTP errors from the token endpoint, signature mismatch, or invalid JSON response from Tuya's token API.","commonSituations":"Rotated or mistyped credentials, wrong region endpoint, clock skew breaking the HMAC signature, network/firewall blocking the cloud endpoint, or Tuya API changes.","solutions":["Verify accessId and accessSecret are correct and unrotated","Check the base URL/region matches your Tuya data center","Ensure system clock is accurate (NTP) since signing is time-sensitive","Unwrap the inner error (errors.Unwrap / %v) to see whether it's network, HTTP status, or JSON, and fix accordingly"],"exampleFix":"// before\nif err := apiClient.Init(); err != nil { return err }\n// after\nif err := apiClient.Init(); err != nil {\n    return fmt.Errorf(\"token init failed (check credentials/region/clock): %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if accessID == \"\" || accessSecret == \"\" { return errors.New(\"missing tuya credentials\") }","typeGuard":null,"tryCatchPattern":"if err := apiClient.Init(); err != nil {\n    var inner error\n    for e := err; e != nil; e = errors.Unwrap(e) { inner = e }\n    log.Printf(\"token init failed: %v (inner: %v)\", err, inner)\n    return err\n}","preventionTips":["Sync system clock via NTP — signatures are time-sensitive","Rotate credentials through config, not code edits","Check Tuya API status/network reachability before diagnosing code"],"tags":["go","tuya","token","authentication","cloud-api"],"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"}