# 百家号 API 调试指南 ## 问题:刷新时没有使用 API 如果你发现百家号刷新时仍然使用浏览器而不是 API,请按以下步骤排查: ### 1. 确认代码已更新 检查 `server/src/services/HeadlessBrowserService.ts` 文件中的 `fetchAccountInfo` 方法: ```typescript async fetchAccountInfo(platform: PlatformType, cookies: CookieData[]): Promise { logger.info(`[fetchAccountInfo] Starting for platform: ${platform}`); // 百家号使用直接 API 获取账号信息和作品列表 if (platform === 'baijiahao') { logger.info(`[fetchAccountInfo] Using direct API for baijiahao`); return this.fetchBaijiahaoAccountInfoDirectApi(cookies); } // ... } ``` **关键点**: - 必须有 `if (platform === 'baijiahao')` 判断 - 必须调用 `fetchBaijiahaoAccountInfoDirectApi` ### 2. 重新编译代码 TypeScript 代码需要编译后才能生效: ```bash # 进入 server 目录 cd server # 重新编译 npm run build # 或者使用开发模式(自动编译) npm run dev ``` ### 3. 重启服务 确保服务已重启: ```bash # 停止服务 # Ctrl+C 或关闭终端 # 重新启动 npm run dev ``` ### 4. 查看日志 刷新百家号账号时,应该看到以下日志: ``` [fetchAccountInfo] Starting for platform: baijiahao [fetchAccountInfo] Using direct API for baijiahao [Baijiahao API] Fetching account info via direct API... [Baijiahao API] Step 1: Fetching appinfo... [Baijiahao API] appinfo response: errno=0, errmsg=success [Baijiahao API] Got account info: name=xxx, id=bjh_xxx [Baijiahao API] Step 2: Fetching growth info... [Baijiahao API] Got fans count: 1234 [Baijiahao API] Step 3: Fetching works list... [Baijiahao API] Successfully fetched account info ``` **如果没有看到这些日志**: - 代码可能没有编译 - 服务可能没有重启 - 可能使用了旧的代码 ### 5. 测试 API 接口 使用测试脚本验证 API 是否可用: ```bash # 在 server 目录下运行 node test-baijiahao-api.js ``` **注意**:需要先修改脚本中的 Cookie 为真实的 Cookie。 ### 6. 检查平台类型 确认账号的 platform 字段是 `baijiahao`: ```sql -- 在数据库中查询 SELECT id, accountName, platform FROM platform_accounts WHERE platform = 'baijiahao'; ``` **如果 platform 不是 `baijiahao`**: - 可能是数据错误 - 需要更新数据库 ### 7. 检查 Cookie 格式 确认 Cookie 数据格式正确: ```typescript // Cookie 应该是 JSON 数组格式 [ { "name": "BDUSS", "value": "xxx", "domain": ".baidu.com", "path": "/" }, { "name": "STOKEN", "value": "xxx", "domain": ".baidu.com", "path": "/" } ] ``` ### 8. 常见问题 #### 问题 1:日志中没有 `[fetchAccountInfo]` **原因**:代码没有编译或服务没有重启 **解决**: 1. 运行 `npm run build` 2. 重启服务 #### 问题 2:日志显示 `[Playwright]` 而不是 `[Baijiahao API]` **原因**:没有进入百家号的特殊处理分支 **解决**: 1. 检查 platform 是否为 `baijiahao` 2. 检查代码中的 if 判断 #### 问题 3:API 返回 errno=110 **原因**:Cookie 无效或已过期 **解决**:重新登录账号 #### 问题 4:编译后仍然使用旧代码 **原因**:可能有缓存 **解决**: 1. 删除 `server/dist` 目录 2. 重新编译:`npm run build` 3. 重启服务 ### 9. 强制使用 API(调试用) 如果仍然有问题,可以临时修改代码强制使用 API: ```typescript async fetchAccountInfo(platform: PlatformType, cookies: CookieData[]): Promise { // 强制所有平台都使用百家号 API(仅用于调试) logger.info(`[DEBUG] Force using baijiahao API for ${platform}`); return this.fetchBaijiahaoAccountInfoDirectApi(cookies); } ``` **注意**:这只是临时调试方法,不要用于生产环境。 ### 10. 验证步骤总结 1. ✅ 代码已更新(有 `if (platform === 'baijiahao')`) 2. ✅ 代码已编译(`npm run build`) 3. ✅ 服务已重启 4. ✅ 数据库中 platform 为 `baijiahao` 5. ✅ Cookie 格式正确 6. ✅ Cookie 未过期 7. ✅ 日志中显示 `[Baijiahao API]` 如果以上步骤都完成了,刷新功能应该会使用 API 接口。 ### 11. 获取详细日志 如果仍然有问题,可以增加日志级别: ```typescript // 在 fetchAccountInfo 开头添加 console.log('=== DEBUG fetchAccountInfo ==='); console.log('platform:', platform); console.log('platform === "baijiahao":', platform === 'baijiahao'); console.log('typeof platform:', typeof platform); console.log('cookies count:', cookies.length); console.log('=============================='); ``` 这样可以看到更详细的调试信息。