test-api-403.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // 测试不同 headers 组合找出 403 的原因
  2. const API_KEY = "ah-52ff02ce8f7c1e6028e3bec33ba488a92cdaec659756c8cee92c2b4ef5ee83fe";
  3. const BASE_URL = "https://hub.linux.do/v1";
  4. async function testWithHeaders(testName, headers) {
  5. console.log(`\n=== ${testName} ===`);
  6. const url = `${BASE_URL}/models`;
  7. try {
  8. const response = await fetch(url, {
  9. method: "GET",
  10. headers: headers
  11. });
  12. console.log(`状态码: ${response.status} ${response.statusText}`);
  13. if (response.status === 403) {
  14. const text = await response.text();
  15. console.log(`403 响应: ${text.substring(0, 200)}`);
  16. } else if (response.ok) {
  17. console.log("✅ 成功");
  18. }
  19. return response.status;
  20. } catch (error) {
  21. console.error(`❌ 错误: ${error.message}`);
  22. return -1;
  23. }
  24. }
  25. async function runTests() {
  26. // 测试 1: 只有 Authorization
  27. await testWithHeaders("测试1: 只有 Authorization", {
  28. "Authorization": `Bearer ${API_KEY}`
  29. });
  30. // 测试 2: Authorization + Content-Type
  31. await testWithHeaders("测试2: Authorization + Content-Type", {
  32. "Authorization": `Bearer ${API_KEY}`,
  33. "Content-Type": "application/json"
  34. });
  35. // 测试 3: Authorization + Origin: 空字符串 (模拟软件中的行为)
  36. await testWithHeaders("测试3: Authorization + Origin: 空", {
  37. "Authorization": `Bearer ${API_KEY}`,
  38. "Origin": ""
  39. });
  40. // 测试 4: Authorization + Origin: http://localhost
  41. await testWithHeaders("测试4: Authorization + Origin: localhost", {
  42. "Authorization": `Bearer ${API_KEY}`,
  43. "Origin": "http://localhost"
  44. });
  45. // 测试 5: Authorization + Content-Type + Origin: 空
  46. await testWithHeaders("测试5: Authorization + Content-Type + Origin: 空", {
  47. "Authorization": `Bearer ${API_KEY}`,
  48. "Content-Type": "application/json",
  49. "Origin": ""
  50. });
  51. // 测试 6: 模拟软件 withCustomOriginHeader 的输出
  52. await testWithHeaders("测试6: 模拟软件的 headers (非本地端点)", {
  53. "Content-Type": "application/json",
  54. "Authorization": `Bearer ${API_KEY}`,
  55. "Origin": "" // 这是软件中对非本地端点设置的
  56. });
  57. // 测试 7: 不设置 Origin
  58. await testWithHeaders("测试7: 不设置 Origin", {
  59. "Content-Type": "application/json",
  60. "Authorization": `Bearer ${API_KEY}`
  61. });
  62. // 测试 8: 删除 Content-Type
  63. await testWithHeaders("测试8: 只有 Authorization + 兼容 headers", {
  64. "Authorization": `Bearer ${API_KEY}`,
  65. "Accept": "application/json",
  66. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) QMaiWrite"
  67. });
  68. }
  69. runTests();