xpzouying/xiaohongshu-mcp · warning

标题长度超过限制

Error message

标题长度超过限制

What it means

PublishContent validates the title against Xiaohongshu's limit of 20 characters (counted by xhsutil.CalcTitleLength, which weights CJK vs ASCII characters). Titles longer than this are rejected locally before any API call. The error is a client-side validation guard, not an upstream API error.

Source

Thrown at service.go:208

		if loginAction.WaitForLogin(ctxTimeout) {
			if err := saveCookies(page); err != nil {
				logrus.Errorf("扫码成功但保存 cookies 失败,会话 #%d: %v", seq, err)
				return
			}
			logrus.Infof("扫码登录成功,cookies 已保存,会话 #%d", seq)
			return
		}

		// 没等到扫码:要么超时,要么被新取的二维码取代
		logrus.Infof("登录会话 #%d 结束,未检测到扫码(超时或已被新的二维码取代)", seq)
	}()
}

// PublishContent 发布内容
func (s *XiaohongshuService) PublishContent(ctx context.Context, req *PublishRequest) (*PublishResponse, error) {
	// 验证标题长度(小红书限制:最大20个字)
	if xhsutil.CalcTitleLength(req.Title) > 20 {
		return nil, fmt.Errorf("标题长度超过限制")
	}

	imagePaths, err := s.processImages(req.Images)
	if err != nil {
		return nil, err
	}

	var scheduleTime *time.Time
	if req.ScheduleAt != "" {
		t, err := time.Parse(time.RFC3339, req.ScheduleAt)
		if err != nil {
			return nil, fmt.Errorf("定时发布时间格式错误,请使用 ISO8601 格式: %v", err)
		}

		// 校验定时发布时间范围:1小时至14天
		now := time.Now()
		minTime := now.Add(1 * time.Hour)
		maxTime := now.Add(14 * 24 * time.Hour)

View on GitHub (pinned to 332d196854)

Solutions

  1. Shorten the title so CalcTitleLength(title) <= 20 before calling PublishContent
  2. Pre-validate with xhsutil.CalcTitleLength and return a friendly message to the end user
  3. Truncate the title programmatically, being careful not to cut multi-byte characters mid-point

Example fix

// before
if len(req.Title) > 20 { ... } // byte count, wrong metric
// after
if xhsutil.CalcTitleLength(req.Title) > 20 {
    return nil, fmt.Errorf("标题长度超过限制(最多20字)")
}
Defensive patterns

Strategy: validation

Validate before calling

if xhsutil.CalcTitleLength(req.Title) > 20 {
    return errors.New("标题最多20字")
}

Type guard

func isValidXhsTitle(title string) bool {
    return title != "" && xhsutil.CalcTitleLength(title) <= 20
}

Try / catch

resp, err := svc.PublishContent(ctx, req)
if err != nil && strings.Contains(err.Error(), "标题长度超过限制") {
    return fmt.Errorf("please shorten the title to 20 characters or fewer: %w", err)
}

Prevention

When it happens

Trigger: Calling PublishContent (or its handlers publishHandler / handlePublishContent) with req.Title whose CalcTitleLength exceeds 20.

Common situations: Long descriptive titles pasted from drafts; counting raw bytes/runes instead of the platform's weighted character count; emoji or mixed CJK/Latin text pushing length over the limit.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/70a99afc68284be9. Report an issue: GitHub.