fish2018/pansou · error
缺少必需的Cookie字段
Error message
缺少必需的Cookie字段: %s
What it means
Raised by initCookieFromAlt when the cookies collected from the alt redirect URL do not include one of the required session fields SUB and SUBP. These two cookies are Weibo's long-lived session identifiers; without them the login is not usable, so the function returns an empty cookie string and this error, which checkQRLoginStatus then wraps as "初始化Cookie失败". The %s names exactly which field is missing.
Solutions
- Read the missing field name from the message and log all cookies received from the alt redirect to see what actually came back.
- Ensure the HTTP client follows redirects and stores cookies for all Weibo domains (.weibo.com, .sina.com.cn) — disable any cookie-domain filtering.
- Retry with a fresh QR code; expired/consumed alt URLs yield incomplete sessions.
- Check Weibo risk-control signals (same IP repeatedly logging in, missing User-Agent) that can suppress SUB issuance.
- If Weibo changed the login contract, update requiredFields / cookie collection logic in initCookieFromAlt.
Example fix
// before
requiredFields := []string{"SUB", "SUBP"}
for _, field := range requiredFields {
if _, exists := allCookies[field]; !exists {
return "", fmt.Errorf("缺少必需的Cookie字段: %s", field)
}
}
// after: follow redirects explicitly and log received cookies for diagnosis
requiredFields := []string{"SUB", "SUBP"}
for _, field := range requiredFields {
if _, exists := allCookies[field]; !exists {
names := make([]string, 0, len(allCookies))
for k := range allCookies {
names = append(names, k)
}
return "", fmt.Errorf("缺少必需的Cookie字段: %s (收到: %v)", field, names)
}
} Defensive patterns
Strategy: validation
Validate before calling
// Go: verify SUB/SUBP present in the cookie jar before calling the login flow's final step
func hasSessionCookies(jar http.CookieJar, u *url.URL) bool {
have := map[string]bool{}
for _, c := range jar.Cookies(u) {
have[c.Name] = true
}
return have["SUB"] && have["SUBP"]
} Type guard
func cookiesComplete(allCookies map[string]string) bool {
_, hasSUB := allCookies["SUB"]
_, hasSUBP := allCookies["SUBP"]
return hasSUB && hasSUBP
} Try / catch
cookie, err := plugin.Login(...) // surfaces 缺少必需的Cookie字段 via wrapper
if err != nil {
if strings.Contains(err.Error(), "缺少必需的Cookie字段") {
// session degraded or cookie collection missed a domain; fresh QR + unrestricted jar
return retryQRLogin(plugin, 1)
}
return err
} Prevention
- Configure the cookie jar to accept Set-Cookie from all Weibo/Sina domains the redirect chain touches.
- Send consistent User-Agent and use HTTPS so Set-Cookie headers are not stripped by proxies.
- Log all received cookie names when SUB/SUBP are missing to spot domain or format changes.
- Retry with a new QR code; a consumed or risk-flagged alt URL yields incomplete sessions.
When it happens
Trigger: After following the alt URL returned by a successful QR scan, the collected cookie jar lacks SUB or SUBP — the redirect chain didn't set it, or the plugin's cookie collection missed the domain/path it was set on.
Common situations: Weibo changed which domains set SUB/SUBP (cookie collection filtered to the wrong host); the scan was confirmed but Weibo issued a degraded session (risk control); alt URL already consumed/expired; HTTP (non-HTTPS) or redirected requests where Set-Cookie was dropped by the client or a proxy.
Related errors
- 初始化Cookie失败
- %w: %s
- login required
- loginResp.Message (dynamic remote login failure message)
- username cannot be empty
AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07).
Data as JSON: /api/errors/d80a617351d36c93.
Report an issue: GitHub.
Appendix: source
Thrown at plugin/weibo/weibo.go:1961
}
for _, cookie := range jar.Cookies(weiboCNURL) {
allCookies[cookie.Name] = cookie.Value
}
fmt.Printf("[Weibo DEBUG] 收集到的Cookie字段: %v\n", func() []string {
keys := make([]string, 0, len(allCookies))
for k := range allCookies {
keys = append(keys, k)
}
return keys
}())
// 检查必需的Cookie字段
requiredFields := []string{"SUB", "SUBP"}
for _, field := range requiredFields {
if _, exists := allCookies[field]; !exists {
fmt.Printf("[Weibo DEBUG] 缺少必需的Cookie字段: %s\n", field)
return "", fmt.Errorf("缺少必需的Cookie字段: %s", field)
} else {
fmt.Printf("[Weibo DEBUG] ✓ 找到必需字段: %s\n", field)
}
}
// 构建Cookie字符串
cookieParts := make([]string, 0, len(allCookies))
for k, v := range allCookies {
cookieParts = append(cookieParts, fmt.Sprintf("%s=%s", k, v))
}
cookieStr := strings.Join(cookieParts, "; ")
fmt.Printf("[Weibo DEBUG] Cookie初始化完成, 总长度: %d, 字段数: %d\n", len(cookieStr), len(allCookies))
return cookieStr, nil
}
func (p *WeiboPlugin) generateHash(input string) string {View on GitHub (pinned to beaa561337)