test-baijiahao-api.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * 测试百家号 API 接口
  3. * 用于验证 API 是否正常工作
  4. */
  5. // 示例 Cookie(需要替换为真实的 Cookie)
  6. const testCookie = 'BDUSS=xxx; STOKEN=xxx';
  7. async function testBaijiahaoAPI() {
  8. console.log('开始测试百家号 API...\n');
  9. const headers = {
  10. 'Accept': 'application/json, text/plain, */*',
  11. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  12. 'Cookie': testCookie,
  13. 'Referer': 'https://baijiahao.baidu.com/builder/rc/home',
  14. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
  15. };
  16. // 1. 测试 appinfo 接口
  17. console.log('1. 测试 appinfo 接口...');
  18. try {
  19. const response = await fetch('https://baijiahao.baidu.com/builder/app/appinfo', {
  20. method: 'GET',
  21. headers,
  22. });
  23. const data = await response.json();
  24. console.log(' 状态码:', response.status);
  25. console.log(' errno:', data.errno);
  26. console.log(' errmsg:', data.errmsg);
  27. if (data.data?.user) {
  28. console.log(' 用户名:', data.data.user.name);
  29. console.log(' app_id:', data.data.user.app_id);
  30. console.log(' ✅ appinfo 接口正常\n');
  31. } else {
  32. console.log(' ❌ 没有用户信息\n');
  33. }
  34. } catch (error) {
  35. console.log(' ❌ 请求失败:', error.message, '\n');
  36. }
  37. // 2. 测试 growth info 接口
  38. console.log('2. 测试 growth info 接口...');
  39. try {
  40. const response = await fetch('https://baijiahao.baidu.com/cms-ui/rights/growth/get_info', {
  41. method: 'GET',
  42. headers,
  43. });
  44. const data = await response.json();
  45. console.log(' 状态码:', response.status);
  46. console.log(' errno:', data.errno);
  47. if (data.data) {
  48. console.log(' 粉丝数:', data.data.total_fans);
  49. console.log(' ✅ growth info 接口正常\n');
  50. } else {
  51. console.log(' ❌ 没有数据\n');
  52. }
  53. } catch (error) {
  54. console.log(' ❌ 请求失败:', error.message, '\n');
  55. }
  56. // 3. 测试 article lists 接口
  57. console.log('3. 测试 article lists 接口...');
  58. try {
  59. const response = await fetch('https://baijiahao.baidu.com/pcui/article/lists?currentPage=1&pageSize=20&search=&type=&collection=&startDate=&endDate=&clearBeforeFetch=false&dynamic=0', {
  60. method: 'GET',
  61. headers,
  62. });
  63. const data = await response.json();
  64. console.log(' 状态码:', response.status);
  65. console.log(' errno:', data.errno);
  66. if (data.data) {
  67. console.log(' 作品数:', data.data.list?.length || 0);
  68. console.log(' 总数:', data.data.total);
  69. console.log(' ✅ article lists 接口正常\n');
  70. } else {
  71. console.log(' ❌ 没有数据\n');
  72. }
  73. } catch (error) {
  74. console.log(' ❌ 请求失败:', error.message, '\n');
  75. }
  76. console.log('测试完成!');
  77. console.log('\n注意:如果所有接口都返回 errno=110 或没有数据,说明 Cookie 无效或已过期。');
  78. }
  79. // 运行测试
  80. testBaijiahaoAPI().catch(console.error);