Sync from GitHub: 00c3e58e64ddd4b4f3b3ae2b7ab69b70bb0c810e
Browse files- run.py +11 -0
- test_auth.py +38 -39
run.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
if __name__ == "__main__":
|
| 5 |
+
print("🚀 启动独立鉴权测试...")
|
| 6 |
+
# 运行测试脚本
|
| 7 |
+
subprocess.run(["python", "test_auth.py"])
|
| 8 |
+
|
| 9 |
+
# 启动原来的 app
|
| 10 |
+
print("\n🚀 启动主程序 app.py ...")
|
| 11 |
+
subprocess.run(["python", "app.py"])
|
test_auth.py
CHANGED
|
@@ -2,67 +2,66 @@ import os
|
|
| 2 |
import logging
|
| 3 |
from google.oauth2.credentials import Credentials
|
| 4 |
from googleapiclient.discovery import build
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
#
|
|
|
|
| 8 |
logging.basicConfig(level=logging.DEBUG)
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
-
def
|
| 12 |
print("\n" + "="*50)
|
| 13 |
-
print("
|
| 14 |
-
print("="*50
|
| 15 |
|
| 16 |
-
# 1. 检查环境变量
|
| 17 |
client_id = os.environ.get("G_CLIENT_ID")
|
| 18 |
client_secret = os.environ.get("G_CLIENT_SECRET")
|
| 19 |
refresh_token = os.environ.get("G_REFRESH_TOKEN")
|
| 20 |
|
| 21 |
-
print(f"Client ID: {client_id[:10]}... (
|
| 22 |
-
print(f"
|
|
|
|
| 23 |
|
| 24 |
if not all([client_id, client_secret, refresh_token]):
|
| 25 |
-
print("
|
| 26 |
return
|
| 27 |
|
| 28 |
-
# 2. 构建凭据
|
| 29 |
-
print("\n🔄 正在构建 Credentials 对象...")
|
| 30 |
-
creds = Credentials(
|
| 31 |
-
token=None,
|
| 32 |
-
refresh_token=refresh_token,
|
| 33 |
-
token_uri="https://oauth2.googleapis.com/token",
|
| 34 |
-
client_id=client_id,
|
| 35 |
-
client_secret=client_secret
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
# 3. 尝试刷新 Token (关键步骤)
|
| 39 |
-
print("⚡ 正在尝试刷新 Access Token (连接 Google)...")
|
| 40 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
from google.auth.transport.requests import Request
|
| 42 |
creds.refresh(Request())
|
| 43 |
-
print(f"
|
| 44 |
-
except google.auth.exceptions.RefreshError as e:
|
| 45 |
-
print(f"\n❌ 刷新失败: {e}")
|
| 46 |
-
print("💡 原因分析:")
|
| 47 |
-
print("1. Refresh Token 已过期 (测试版应用7天过期)")
|
| 48 |
-
print("2. Refresh Token 与 Client ID 不匹配 (必须是一套)")
|
| 49 |
-
print("3. Refresh Token 被手动撤销")
|
| 50 |
-
return
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"\n❌ 网络连接失败: {e}")
|
| 53 |
-
return
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
service = build("drive", "v3", credentials=creds)
|
|
|
|
|
|
|
| 59 |
about = service.about().get(fields="user").execute()
|
| 60 |
user_info = about.get('user', {})
|
| 61 |
-
|
| 62 |
-
print(
|
|
|
|
|
|
|
| 63 |
print(f"📧 邮箱: {user_info.get('emailAddress')}")
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
|
|
|
|
| 2 |
import logging
|
| 3 |
from google.oauth2.credentials import Credentials
|
| 4 |
from googleapiclient.discovery import build
|
| 5 |
+
import http.client
|
| 6 |
|
| 7 |
+
# 开启调试日志
|
| 8 |
+
http.client.HTTPConnection.debuglevel = 1
|
| 9 |
logging.basicConfig(level=logging.DEBUG)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
+
def test_auth_only():
|
| 13 |
print("\n" + "="*50)
|
| 14 |
+
print("🛠️ 正在执行纯鉴权测试 (Test Auth Only)")
|
| 15 |
+
print("="*50)
|
| 16 |
|
|
|
|
| 17 |
client_id = os.environ.get("G_CLIENT_ID")
|
| 18 |
client_secret = os.environ.get("G_CLIENT_SECRET")
|
| 19 |
refresh_token = os.environ.get("G_REFRESH_TOKEN")
|
| 20 |
|
| 21 |
+
print(f"Client ID: {client_id[:10]}... (Len: {len(str(client_id))})")
|
| 22 |
+
print(f"Client Secret: {client_secret[:5]}... (Len: {len(str(client_secret))})")
|
| 23 |
+
print(f"Refresh Token: {refresh_token[:10]}... (Len: {len(str(refresh_token))})")
|
| 24 |
|
| 25 |
if not all([client_id, client_secret, refresh_token]):
|
| 26 |
+
print("❌ 错误: 环境变量缺失")
|
| 27 |
return
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
+
creds = Credentials(
|
| 31 |
+
token=None,
|
| 32 |
+
refresh_token=refresh_token,
|
| 33 |
+
token_uri="https://oauth2.googleapis.com/token",
|
| 34 |
+
client_id=client_id,
|
| 35 |
+
client_secret=client_secret
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# 强制刷新 Token,这是最直接的验证方式
|
| 39 |
+
print("\n🔄 正在尝试刷新 Access Token...")
|
| 40 |
from google.auth.transport.requests import Request
|
| 41 |
creds.refresh(Request())
|
| 42 |
+
print(f"✅ Token 刷新成功! 新 Access Token: {creds.token[:10]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# 构建 Service 并调用简单的 API
|
| 45 |
+
print("\n📡 正在连接 Google Drive API...")
|
|
|
|
| 46 |
service = build("drive", "v3", credentials=creds)
|
| 47 |
+
|
| 48 |
+
print("👤 正在获取用户信息 (about.get)...")
|
| 49 |
about = service.about().get(fields="user").execute()
|
| 50 |
user_info = about.get('user', {})
|
| 51 |
+
|
| 52 |
+
print("\n" + "="*50)
|
| 53 |
+
print(f"✅ 鉴权完美通过!")
|
| 54 |
+
print(f"👋 用户名: {user_info.get('displayName')}")
|
| 55 |
print(f"📧 邮箱: {user_info.get('emailAddress')}")
|
| 56 |
+
print("="*50)
|
| 57 |
+
|
| 58 |
except Exception as e:
|
| 59 |
+
print("\n" + "="*50)
|
| 60 |
+
print(f"❌ 鉴权测试失败!")
|
| 61 |
+
print(f"错误信息: {e}")
|
| 62 |
+
print("="*50)
|
| 63 |
+
import traceback
|
| 64 |
+
traceback.print_exc()
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
test_auth_only()
|