RayWangQvQ/BiliBiliToolPro · error · BiliValidationException

[ ]= 不能转换为long型,请确认配置的是正确的Cookie值

Error message

[{0}]={1} 不能转换为long型,请确认配置的是正确的Cookie值

What it means

A BiliValidationException thrown from BiliCookie.Check() during cookie validation. The UserId cookie value, read from the configured BiliBili cookie string, failed its long.TryParse conversion — meaning the stored DEDEUserID value is non-numeric, malformed, or empty, so the cookie configuration is invalid and the tool refuses to proceed. This is a validation guard on the user-supplied cookie config: the offending input is the UserId entry of the cookie string (e.g. a truncated, edited, or wrong-field value in place of the numeric DEDEUserID). Check() is exercised by the Check_* test suite (missing/empty dictionary, missing UserId, non-numeric UserId, missing SessData, missing BiliJct).

Solutions

  1. Add bili_jct=<value> to the cookie string, copied from browser devtools.
  2. Confirm you copied cookies while logged in to bilibili.
  3. Verify all three required items (DedeUserID, SESSDATA, bili_jct) exist before running.

Example fix

// before
var cookie = new BiliCookie("DedeUserID=12345; SESSDATA=abc");
// after
var cookie = new BiliCookie("DedeUserID=12345; SESSDATA=abc; bili_jct=6789token");
Defensive patterns

Strategy: validation

Validate before calling

if (!cookieString.Contains("bili_jct="))
    throw new ArgumentException("Cookie 缺少 bili_jct (CSRF token)");

Try / catch

try { cookie.Check(); }
catch (BiliValidationException ex) { logger.LogError("Cookie 缺少 bili_jct: {Msg}", ex.Message); }

Prevention

When it happens

Trigger: Cookie string parsed but no bili_jct key/value; tests passing dictionaries without BiliJct.

Common situations: Partial cookie copy omitting bili_jct; confusing bili_jct with SESSDATA and providing only one; cookie exported from a logged-out browser session where bili_jct does not exist.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of RayWangQvQ/BiliBiliToolPro@c599b2c0da (2026-09-12). Data as JSON: /api/errors/2ef3ad072e91700a. Report an issue: GitHub.

Appendix: source

Thrown at src/Ray.BiliBiliTool.Agent/BiliCookie.cs:106

                    "[{0}]={1} 不能转换为long型,请确认配置的是正确的Cookie值",
                    GetPropertyDescription(nameof(UserId)),
                    UserId
                )
            );
        }

        //SessData为空,抛异常
        if (string.IsNullOrWhiteSpace(SessData))
        {
            throw new BiliValidationException(
                string.Format(msg, GetPropertyDescription(nameof(SessData)))
            );
        }

        //BiliJct为空,抛异常
        if (string.IsNullOrWhiteSpace(BiliJct))
        {
            throw new BiliValidationException(
                string.Format(msg, GetPropertyDescription(nameof(BiliJct)))
            );
        }

        if (!result)
            throw new BiliValidationException(
                $"请正确配置Cookie后再运行,配置方式见 {Config.Constants.SourceCodeUrl}"
            );
    }

    private string GetPropertyDescription(string propertyName)
    {
        return GetType().GetPropertyDescription(propertyName);
    }
}

View on GitHub (pinned to c599b2c0da)