reposync/debug_gitlink_api.py

239 lines
7.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Gitlink API 详细调试脚本
测试不同的API端点和参数组合
"""
import requests
import json
import urllib3
# 禁用SSL警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def test_api_endpoints():
"""测试不同的API端点"""
print("=== 测试Gitlink API端点 ===")
base_url = "https://www.gitlink.org.cn"
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
headers = {
'Cookie': cookie,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# 测试不同的API端点
endpoints = [
"/api/v1/user",
"/api/v1/projects",
"/api/v1/repos",
"/api/v1/repositories",
"/api/v1/user/projects",
"/api/v1/user/repos",
"/api/v1/user/repositories"
]
for endpoint in endpoints:
url = base_url + endpoint
print(f"\n测试端点: {endpoint}")
try:
response = requests.get(url, headers=headers, timeout=30, verify=False)
print(f"状态码: {response.status_code}")
print(f"内容类型: {response.headers.get('Content-Type', 'N/A')}")
print(f"响应内容: {response.text[:200]}...")
if response.status_code == 200:
try:
data = response.json()
print(f"JSON格式: {type(data)}")
if isinstance(data, dict):
print(f"键: {list(data.keys())}")
except:
print("非JSON格式")
except Exception as e:
print(f"异常: {e}")
def test_project_search():
"""测试项目搜索的不同方法"""
print("\n=== 测试项目搜索方法 ===")
base_url = "https://www.gitlink.org.cn"
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
owner = "gonggong123zzz"
repo_name = "repo2"
headers = {
'Cookie': cookie,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# 方法1: 通过查询参数搜索
print("\n方法1: 通过查询参数搜索")
url1 = f"{base_url}/api/v1/projects"
params1 = {'owner': owner, 'name': repo_name}
try:
response = requests.get(url1, params=params1, headers=headers, timeout=30, verify=False)
print(f"URL: {response.url}")
print(f"状态码: {response.status_code}")
print(f"响应: {response.text}")
except Exception as e:
print(f"异常: {e}")
# 方法2: 通过路径参数搜索
print("\n方法2: 通过路径参数搜索")
endpoints = [
f"{base_url}/api/v1/projects/{owner}/{repo_name}",
f"{base_url}/api/v1/repos/{owner}/{repo_name}",
f"{base_url}/api/v1/repositories/{owner}/{repo_name}"
]
for endpoint in endpoints:
print(f"\n尝试: {endpoint}")
try:
response = requests.get(endpoint, headers=headers, timeout=30, verify=False)
print(f"状态码: {response.status_code}")
print(f"内容类型: {response.headers.get('Content-Type', 'N/A')}")
if response.status_code == 200:
print(f"成功: {response.text[:200]}...")
else:
print(f"失败: {response.text[:100]}...")
except Exception as e:
print(f"异常: {e}")
def test_user_projects():
"""测试获取用户项目列表"""
print("\n=== 测试用户项目列表 ===")
base_url = "https://www.gitlink.org.cn"
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
headers = {
'Cookie': cookie,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
endpoints = [
"/api/v1/user/projects",
"/api/v1/user/repos",
"/api/v1/user/repositories"
]
for endpoint in endpoints:
url = base_url + endpoint
print(f"\n测试: {endpoint}")
try:
response = requests.get(url, headers=headers, timeout=30, verify=False)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
try:
data = response.json()
if isinstance(data, list):
print(f"找到 {len(data)} 个项目")
if data:
print(f"第一个项目: {json.dumps(data[0], indent=2, ensure_ascii=False)}")
else:
print(f"响应格式: {type(data)}")
print(f"内容: {json.dumps(data, indent=2, ensure_ascii=False)}")
except:
print(f"非JSON格式: {response.text[:200]}...")
else:
print(f"失败: {response.text}")
except Exception as e:
print(f"异常: {e}")
def test_auth_methods():
"""测试不同的认证方法"""
print("\n=== 测试认证方法 ===")
base_url = "https://www.gitlink.org.cn"
cookie = "autologin_trustie=4a98f5358aab9fc87f9bdb6332dc87ec337dc8cf"
# 方法1: 只使用Cookie
print("\n方法1: 只使用Cookie")
headers1 = {'Cookie': cookie}
try:
response = requests.get(f"{base_url}/api/v1/user", headers=headers1, timeout=30, verify=False)
print(f"状态码: {response.status_code}")
print(f"响应: {response.text}")
except Exception as e:
print(f"异常: {e}")
# 方法2: 使用Cookie + JSON头
print("\n方法2: 使用Cookie + JSON头")
headers2 = {
'Cookie': cookie,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
try:
response = requests.get(f"{base_url}/api/v1/user", headers=headers2, timeout=30, verify=False)
print(f"状态码: {response.status_code}")
print(f"响应: {response.text}")
except Exception as e:
print(f"异常: {e}")
def test_web_interface():
"""测试Web界面访问"""
print("\n=== 测试Web界面访问 ===")
base_url = "https://www.gitlink.org.cn"
owner = "gonggong123zzz"
repo_name = "repo2"
# 测试项目页面
project_url = f"{base_url}/{owner}/{repo_name}"
print(f"项目页面: {project_url}")
try:
response = requests.get(project_url, timeout=30, verify=False)
print(f"状态码: {response.status_code}")
if response.status_code == 200:
print("✓ 项目页面存在")
# 检查页面内容是否包含项目信息
if repo_name.lower() in response.text.lower():
print("✓ 页面包含项目名称")
else:
print("✗ 页面不包含项目名称")
else:
print("✗ 项目页面不存在")
except Exception as e:
print(f"异常: {e}")
def main():
"""主函数"""
print("Gitlink API 详细调试工具")
print("=" * 60)
# 1. 测试API端点
test_api_endpoints()
# 2. 测试项目搜索
test_project_search()
# 3. 测试用户项目列表
test_user_projects()
# 4. 测试认证方法
test_auth_methods()
# 5. 测试Web界面
test_web_interface()
print("\n" + "=" * 60)
print("调试完成")
print("\n分析结果:")
print("1. 检查认证是否有效")
print("2. 确认项目是否存在")
print("3. 验证API端点是否正确")
print("4. 检查网络连接")
if __name__ == "__main__":
main()