diagnose_api.py
3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
"""Feishu API 诊断脚本 — 测 token、IM、bitable 是否都通。
用法: python3 scripts/diagnose_api.py
会从 ~/.hermes/.env 或环境变量读取 FEISHU_APP_SECRET。
"""
import json, urllib.request, urllib.error, os, sys
def get_secret():
"""从 .env 文件或环境变量读取 APP_SECRET"""
env_secret = os.environ.get("FEISHU_APP_SECRET", "")
if env_secret:
return env_secret
env_path = os.path.expanduser("~/.hermes/.env")
try:
with open(env_path) as f:
for line in f:
if line.startswith("FEISHU_APP_SECRET="):
return line.strip().split("=", 1)[1]
except Exception:
pass
return ""
APP_ID = "cli_a91b01debab81cd1"
APP_SECRET = get_secret()
if not APP_SECRET:
print("❌ 未找到 FEISHU_APP_SECRET(检查 ~/.hermes/.env 或环境变量)")
sys.exit(1)
# ── 1. 获取 token ──
print("=" * 50)
print("1. 获取 tenant_access_token")
data = json.dumps({"app_id": APP_ID, "app_secret": APP_SECRET}).encode()
req = urllib.request.Request(
"https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal",
data=data, headers={"Content-Type": "application/json"}, method="POST"
)
try:
with urllib.request.urlopen(req, timeout=15) as r:
resp = json.loads(r.read())
if resp.get("code") == 0:
token = resp["tenant_access_token"]
print(f" ✅ Token: {token[:20]}... (expires in {resp.get('expire', '?')}s)")
else:
print(f" ❌ code={resp.get('code')}: {resp.get('msg')}")
sys.exit(1)
except urllib.error.HTTPError as e:
print(f" ❌ HTTP {e.code}: {e.read().decode()[:200]}")
sys.exit(1)
auth = {"Authorization": f"Bearer {token}"}
# ── 2. 测试 IM API ──
print("=" * 50)
print("2. 测试 IM API(基准:应返回 code:0)")
try:
req2 = urllib.request.Request(
"https://open.feishu.cn/open-apis/im/v1/chats?page_size=1",
headers=auth
)
with urllib.request.urlopen(req2, timeout=15) as r:
im_resp = json.loads(r.read())
if im_resp.get("code") == 0:
print(f" ✅ IM 通路正常 (code=0)")
else:
print(f" ⚠️ IM code={im_resp.get('code')}: {im_resp.get('msg')}")
except urllib.error.HTTPError as e:
print(f" ❌ IM HTTP {e.code}: {e.read().decode()[:200]}")
# ── 3. 测试 Bitable API ──
print("=" * 50)
print("3. 测试多维表格 API (bitable)")
try:
req3 = urllib.request.Request(
"https://open.feishu.cn/open-apis/bitable/v1/apps?page_size=5",
headers=auth
)
with urllib.request.urlopen(req3, timeout=15) as r:
bitable_resp = json.loads(r.read())
if bitable_resp.get("code") == 0:
items = bitable_resp.get("data", {}).get("items", [])
print(f" ✅ Bitable 通路正常,共 {len(items)} 个多维表格")
for item in items:
print(f" 📊 {item['name']} → {item['app_token']}")
else:
print(f" ⚠️ Bitable code={bitable_resp.get('code')}: {bitable_resp.get('msg')}")
except urllib.error.HTTPError as e:
body = e.read().decode()
if e.code == 404:
print(f" ❌ HTTP 404 — 应用未开通 bitable:app 权限")
print(f" 👉 去飞书开放平台 → 应用 {APP_ID} → 权限管理 → 添加 bitable:app")
else:
print(f" ❌ HTTP {e.code}: {body[:200]}")
print("=" * 50)
print("诊断完成。")