Files
wecom_it_smart_desk/docs/03-技术架构/02-技术方案/test_wecom_approval.py
T

78 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""企微审批API权限测试脚本 - 服务器版本(使用 urllib)"""
import urllib.request
import urllib.parse
import urllib.error
import json
import sys
CORP_ID = "wwa8c87970b2011f41"
CORP_SECRET = "EOtQslW7WD8Rna8Nm9WnwCW-ozHP3tustL4mFnet6O8"
print("=" * 60)
print("企微审批API权限测试")
print("=" * 60)
# 1. 获取 access_token
print("\n[1/2] 获取 access_token...")
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={CORP_SECRET}"
try:
with urllib.request.urlopen(url, timeout=10) as response:
result = json.loads(response.read().decode('utf-8'))
except Exception as e:
print(f"❌ 请求失败: {e}")
sys.exit(1)
print(f" 返回: {result}")
if result.get("errcode") != 0:
print(f"❌ access_token 获取失败: {result.get('errmsg')}")
sys.exit(1)
token = result.get("access_token")
print(f"✅ access_token: {token[:20]}...")
# 2. 测试审批API - 企微正确的API路径
print("\n[2/2] 测试审批API...")
# 企微OA审批接口正确的调用方式
# 首先尝试 approvallist 接口
test_endpoints = [
("/cgi-bin/oa/approvallist", {"start_time": 0, "end_time": 9999999999, "cursor": 0, "size": 1}),
("/cgi-bin/oa/approvalinfo", {"spno": "test"}), # 用一个测试单号
]
success = False
for endpoint, params in test_endpoints:
full_url = f"https://qyapi.weixin.qq.com{endpoint}?access_token={token}&{urllib.parse.urlencode(params)}"
print(f" 测试: {endpoint}...")
try:
with urllib.request.urlopen(full_url, timeout=10) as response:
result = json.loads(response.read().decode('utf-8'))
except urllib.error.HTTPError as e:
result = {"errcode": e.code, "errmsg": f"HTTP {e.code}"}
except Exception as e:
result = {"errcode": -1, "errmsg": str(e)}
print(f" errcode: {result.get('errcode')}, errmsg: {result.get('errmsg')}")
if result.get("errcode") == 0:
success = True
print(f"{endpoint} 接口可用!")
break
elif result.get("errcode") == 48001:
print(f" ❌ 没有权限: {result.get('errmsg')}")
elif result.get("errcode") == 301012:
print(f" ⚠️ 接口可用但审批单不存在(正常)")
success = True
break
print("\n" + "=" * 60)
if success:
print("✅ 测试结果: 企微审批API权限已开通")
else:
print("❌ 测试结果: 未开通审批API权限 (错误码 48001)")
print("=" * 60)