| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #!/usr/bin/env python3
- """
- 测试OAuth2端点
- """
- import requests
- import json
- def test_oauth_authorize():
- """测试OAuth2授权端点"""
- url = "http://localhost:8000/oauth/authorize"
-
- params = {
- "response_type": "code",
- "client_id": "eqhoIdAyAWbA8MsYHsNqQqNLJbCayTjY",
- "redirect_uri": "http://localhost:8001/auth/callback",
- "scope": "profile email",
- "state": "test_state_123"
- }
-
- print("测试OAuth2授权端点...")
- print(f"URL: {url}")
- print(f"参数: {json.dumps(params, indent=2)}")
-
- try:
- response = requests.get(url, params=params, timeout=10, allow_redirects=False)
- print(f"\n状态码: {response.status_code}")
- print(f"响应头: {dict(response.headers)}")
-
- if response.status_code == 200:
- print("✅ 授权端点正常工作!")
- print("返回了授权页面HTML")
- elif response.status_code == 302:
- print("✅ 受信任应用自动重定向!")
- print(f"重定向到: {response.headers.get('location')}")
- else:
- print(f"❌ 意外的状态码: {response.status_code}")
- print(f"响应内容: {response.text}")
-
- except requests.exceptions.ConnectionError:
- print("\n❌ 无法连接到服务器,请确保后端服务正在运行")
- except Exception as e:
- print(f"\n❌ 测试失败: {e}")
- def test_oauth_token():
- """测试OAuth2令牌端点"""
- url = "http://localhost:8000/oauth/token"
-
- data = {
- "grant_type": "authorization_code",
- "code": "test_auth_code_123",
- "redirect_uri": "http://localhost:8001/auth/callback",
- "client_id": "eqhoIdAyAWbA8MsYHsNqQqNLJbCayTjY",
- "client_secret": "LKJm5XHJFhhgxSv9nQhoQNNI3wrKyWGZCaPQ4qc43Lf5qfXdLAHoGAHhCYqApEpr"
- }
-
- print("\n" + "="*50)
- print("测试OAuth2令牌端点...")
- print(f"URL: {url}")
- print(f"数据: {json.dumps(data, indent=2)}")
-
- try:
- response = requests.post(url, data=data, timeout=10)
- print(f"\n状态码: {response.status_code}")
-
- if response.status_code == 200:
- result = response.json()
- print("✅ 令牌端点正常工作!")
- print(f"访问令牌: {result.get('access_token', '')[:50]}...")
- print(f"令牌类型: {result.get('token_type')}")
- print(f"过期时间: {result.get('expires_in')} 秒")
- else:
- print(f"❌ 令牌请求失败: {response.status_code}")
- print(f"响应内容: {response.text}")
-
- except Exception as e:
- print(f"\n❌ 测试失败: {e}")
- if __name__ == "__main__":
- test_oauth_authorize()
- test_oauth_token()
|