{"record":{"id":"60ccba26085c0d68","repo":"fish2018/pansou","slug":"cookie-v","errorCode":null,"errorMessage":"初始化Cookie失败: %v","messagePattern":"初始化Cookie失败: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugin/weibo/weibo.go","lineNumber":1757,"sourceCode":"\t}\n\t\n\tfmt.Printf(\"[Weibo DEBUG] 解析后retcode: %d, msg: %s\\n\", result.Retcode, result.Msg)\n\t\n\t// 参考Python auto.py第93-108行的状态码处理\n\t// 20000000: 扫码成功\n\t// 50114001: 等待扫码\n\t// 50114002: 已扫描，等待确认\n\t// 50114004: 二维码已过期\n\t\n\tif result.Retcode == 20000000 {\n\t\t// 登录成功，需要初始化Cookie\n\t\talt := result.Data.URL\n\t\tfmt.Printf(\"[Weibo DEBUG] 登录成功! alt URL: %s\\n\", alt)\n\t\t\n\t\tcookieStr, err := p.initCookieFromAlt(alt)\n\t\tif err != nil {\n\t\t\tfmt.Printf(\"[Weibo DEBUG] 初始化Cookie失败: %v\\n\", err)\n\t\t\treturn nil, fmt.Errorf(\"初始化Cookie失败: %v\", err)\n\t\t}\n\t\t\n\t\tfmt.Printf(\"[Weibo DEBUG] Cookie初始化成功, Cookie长度: %d\\n\", len(cookieStr))\n\t\treturn &LoginResult{Status: \"success\", Cookie: cookieStr}, nil\n\t} else if result.Retcode == 50114002 {\n\t\t// 已扫描，等待确认\n\t\tfmt.Printf(\"[Weibo DEBUG] 已扫描，等待确认\\n\")\n\t\treturn &LoginResult{Status: \"waiting\", Message: \"已扫描，请在手机上确认\"}, nil\n\t} else if result.Retcode == 50114004 {\n\t\t// 二维码已过期\n\t\tfmt.Printf(\"[Weibo DEBUG] 二维码已过期\\n\")\n\t\treturn &LoginResult{Status: \"expired\", Message: \"二维码已过期\"}, nil\n\t}\n\t\n\t// 默认状态：等待扫码\n\tfmt.Printf(\"[Weibo DEBUG] 等待扫码中, retcode: %d\\n\", result.Retcode)\n\treturn &LoginResult{Status: \"waiting\", Message: \"等待扫码中\"}, nil\n}","sourceCodeStart":1739,"sourceCodeEnd":1775,"githubUrl":"https://github.com/fish2018/pansou/blob/beaa56133755a548ebc51b090b3816e2ae044aa6/plugin/weibo/weibo.go#L1739-L1775","documentation":"This error wraps a failure from initCookieFromAlt() during QR-code login in the Weibo plugin. After the user scans and confirms, checkQRLoginStatus extracts the 'alt' redirect URL and exchanges it for session cookies; if that exchange fails (network error, non-2xx response, or missing SUB/SUBP cookies), the error is wrapped as \"初始化Cookie失败\" (\"cookie initialization failed\") and aborts the login. It is a wrapper, so the root cause is always the inner error (%v).","triggerScenarios":"checkQRLoginStatus polls the login status, receives retcode indicating success plus result.Data.URL, then calls initCookieFromAlt(alt) which fails — e.g. the alt URL request returns an error or the response cookies lack required fields. Called from handleCheckLogin.","commonSituations":"Weibo changed its login redirect/cookie contract so SUB or SUBP is no longer set; the alt URL expired because polling was too slow; network/proxy failures hitting the redirect endpoint; Weibo anti-bot measures returning an HTML challenge page instead of setting cookies.","solutions":["Inspect the wrapped inner error (%v) in the message to find the real cause (network vs missing cookie field).","If it reports a missing cookie field (e.g. SUB/SUBP), verify the Weibo login flow is unchanged and re-scan a fresh QR code quickly after generating it.","Retry the login with a newly generated QR code; alt URLs are short-lived.","Check network/proxy/VPN settings that could block v2.qr.weibo.cn or weibo.com redirect requests.","Update the plugin: if Weibo changed the cookie contract, initCookieFromAlt may need patched parsing or required fields."],"exampleFix":"// before\ncookieStr, err := p.initCookieFromAlt(alt)\nif err != nil {\n    return nil, fmt.Errorf(\"初始化Cookie失败: %v\", err)\n}\n// after: retry once with fresh status poll before giving up\ncookieStr, err := p.initCookieFromAlt(alt)\nif err != nil {\n    time.Sleep(2 * time.Second)\n    result, err = p.checkQRLoginStatus(...) // re-poll to get a fresh alt URL\n    if err == nil && result != nil {\n        cookieStr, err = p.initCookieFromAlt(result.Data.URL)\n    }\n    if err != nil {\n        return nil, fmt.Errorf(\"初始化Cookie失败: %v\", err)\n    }\n}","handlingStrategy":"try-catch","validationCode":"// Go: pre-check that a login status result carries an alt URL before exchanging for cookies\nif result == nil || result.Data.URL == \"\" {\n    return fmt.Errorf(\"no alt URL available; QR login not completed yet\")\n}","typeGuard":"func hasAltURL(result *LoginStatus) bool {\n    return result != nil && result.Data != nil && result.Data.URL != \"\"\n}","tryCatchPattern":"res, err := plugin.CheckLogin(...) // handleCheckLogin path\nif err != nil {\n    if strings.Contains(err.Error(), \"初始化Cookie失败\") {\n        // regenerate QR and retry once with a fresh alt URL\n        return retryQRLogin(plugin, 1)\n    }\n    return err\n}","preventionTips":["Exchange the alt URL for cookies immediately after success is detected; alt URLs expire quickly.","Use an HTTP client that follows redirects and keeps cookies across all Weibo domains.","Always log the wrapped inner error, not just the wrapper message.","Regenerate a fresh QR code on failure instead of reusing a consumed one."],"tags":["weibo","qr-login","cookie","authentication"],"backgroundTag":"missing-credentials","analyzedSha":"beaa56133755a548ebc51b090b3816e2ae044aa6","analyzedAt":"2026-09-07T00:31:18.025Z","contentChangedAt":"2026-09-07T00:31:18.025Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}