fish2018/pansou · error

初始化Cookie失败

Error message

初始化Cookie失败: %v

What it means

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).

Solutions

  1. Inspect the wrapped inner error (%v) in the message to find the real cause (network vs missing cookie field).
  2. 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.
  3. Retry the login with a newly generated QR code; alt URLs are short-lived.
  4. Check network/proxy/VPN settings that could block v2.qr.weibo.cn or weibo.com redirect requests.
  5. Update the plugin: if Weibo changed the cookie contract, initCookieFromAlt may need patched parsing or required fields.

Example fix

// before
cookieStr, err := p.initCookieFromAlt(alt)
if err != nil {
    return nil, fmt.Errorf("初始化Cookie失败: %v", err)
}
// after: retry once with fresh status poll before giving up
cookieStr, err := p.initCookieFromAlt(alt)
if err != nil {
    time.Sleep(2 * time.Second)
    result, err = p.checkQRLoginStatus(...) // re-poll to get a fresh alt URL
    if err == nil && result != nil {
        cookieStr, err = p.initCookieFromAlt(result.Data.URL)
    }
    if err != nil {
        return nil, fmt.Errorf("初始化Cookie失败: %v", err)
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: pre-check that a login status result carries an alt URL before exchanging for cookies
if result == nil || result.Data.URL == "" {
    return fmt.Errorf("no alt URL available; QR login not completed yet")
}

Type guard

func hasAltURL(result *LoginStatus) bool {
    return result != nil && result.Data != nil && result.Data.URL != ""
}

Try / catch

res, err := plugin.CheckLogin(...) // handleCheckLogin path
if err != nil {
    if strings.Contains(err.Error(), "初始化Cookie失败") {
        // regenerate QR and retry once with a fresh alt URL
        return retryQRLogin(plugin, 1)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of fish2018/pansou@beaa561337 (2026-09-07). Data as JSON: /api/errors/60ccba26085c0d68. Report an issue: GitHub.

Appendix: source

Thrown at plugin/weibo/weibo.go:1757

	}
	
	fmt.Printf("[Weibo DEBUG] 解析后retcode: %d, msg: %s\n", result.Retcode, result.Msg)
	
	// 参考Python auto.py第93-108行的状态码处理
	// 20000000: 扫码成功
	// 50114001: 等待扫码
	// 50114002: 已扫描,等待确认
	// 50114004: 二维码已过期
	
	if result.Retcode == 20000000 {
		// 登录成功,需要初始化Cookie
		alt := result.Data.URL
		fmt.Printf("[Weibo DEBUG] 登录成功! alt URL: %s\n", alt)
		
		cookieStr, err := p.initCookieFromAlt(alt)
		if err != nil {
			fmt.Printf("[Weibo DEBUG] 初始化Cookie失败: %v\n", err)
			return nil, fmt.Errorf("初始化Cookie失败: %v", err)
		}
		
		fmt.Printf("[Weibo DEBUG] Cookie初始化成功, Cookie长度: %d\n", len(cookieStr))
		return &LoginResult{Status: "success", Cookie: cookieStr}, nil
	} else if result.Retcode == 50114002 {
		// 已扫描,等待确认
		fmt.Printf("[Weibo DEBUG] 已扫描,等待确认\n")
		return &LoginResult{Status: "waiting", Message: "已扫描,请在手机上确认"}, nil
	} else if result.Retcode == 50114004 {
		// 二维码已过期
		fmt.Printf("[Weibo DEBUG] 二维码已过期\n")
		return &LoginResult{Status: "expired", Message: "二维码已过期"}, nil
	}
	
	// 默认状态:等待扫码
	fmt.Printf("[Weibo DEBUG] 等待扫码中, retcode: %d\n", result.Retcode)
	return &LoginResult{Status: "waiting", Message: "等待扫码中"}, nil
}

View on GitHub (pinned to beaa561337)