| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /**
- * 测试百家号 API 接口
- * 用于验证 API 是否正常工作
- */
- // 示例 Cookie(需要替换为真实的 Cookie)
- const testCookie = 'BDUSS=xxx; STOKEN=xxx';
- async function testBaijiahaoAPI() {
- console.log('开始测试百家号 API...\n');
- const headers = {
- 'Accept': 'application/json, text/plain, */*',
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
- 'Cookie': testCookie,
- 'Referer': 'https://baijiahao.baidu.com/builder/rc/home',
- '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',
- };
- // 1. 测试 appinfo 接口
- console.log('1. 测试 appinfo 接口...');
- try {
- const response = await fetch('https://baijiahao.baidu.com/builder/app/appinfo', {
- method: 'GET',
- headers,
- });
-
- const data = await response.json();
- console.log(' 状态码:', response.status);
- console.log(' errno:', data.errno);
- console.log(' errmsg:', data.errmsg);
- if (data.data?.user) {
- console.log(' 用户名:', data.data.user.name);
- console.log(' app_id:', data.data.user.app_id);
- console.log(' ✅ appinfo 接口正常\n');
- } else {
- console.log(' ❌ 没有用户信息\n');
- }
- } catch (error) {
- console.log(' ❌ 请求失败:', error.message, '\n');
- }
- // 2. 测试 growth info 接口
- console.log('2. 测试 growth info 接口...');
- try {
- const response = await fetch('https://baijiahao.baidu.com/cms-ui/rights/growth/get_info', {
- method: 'GET',
- headers,
- });
-
- const data = await response.json();
- console.log(' 状态码:', response.status);
- console.log(' errno:', data.errno);
- if (data.data) {
- console.log(' 粉丝数:', data.data.total_fans);
- console.log(' ✅ growth info 接口正常\n');
- } else {
- console.log(' ❌ 没有数据\n');
- }
- } catch (error) {
- console.log(' ❌ 请求失败:', error.message, '\n');
- }
- // 3. 测试 article lists 接口
- console.log('3. 测试 article lists 接口...');
- try {
- const response = await fetch('https://baijiahao.baidu.com/pcui/article/lists?currentPage=1&pageSize=20&search=&type=&collection=&startDate=&endDate=&clearBeforeFetch=false&dynamic=0', {
- method: 'GET',
- headers,
- });
-
- const data = await response.json();
- console.log(' 状态码:', response.status);
- console.log(' errno:', data.errno);
- if (data.data) {
- console.log(' 作品数:', data.data.list?.length || 0);
- console.log(' 总数:', data.data.total);
- console.log(' ✅ article lists 接口正常\n');
- } else {
- console.log(' ❌ 没有数据\n');
- }
- } catch (error) {
- console.log(' ❌ 请求失败:', error.message, '\n');
- }
- console.log('测试完成!');
- console.log('\n注意:如果所有接口都返回 errno=110 或没有数据,说明 Cookie 无效或已过期。');
- }
- // 运行测试
- testBaijiahaoAPI().catch(console.error);
|