61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Verify WeCom contact sync works after IP whitelist configuration."""
|
||
|
|
import httpx
|
||
|
|
import json
|
||
|
|
|
||
|
|
CORP_ID = "wwa8c87970b2011f41"
|
||
|
|
CONTACT_SECRET = "BM6iosc3gKnPqkEXmsQN3ErJUpfO-whfMUN646eezB8"
|
||
|
|
|
||
|
|
# Step 1: Get contact access token
|
||
|
|
print("=" * 60)
|
||
|
|
print("Step 1: Get contact access token")
|
||
|
|
resp = httpx.get(f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CORP_ID}&corpsecret={CONTACT_SECRET}", timeout=10)
|
||
|
|
token_data = resp.json()
|
||
|
|
print(f" errcode: {token_data.get('errcode')}")
|
||
|
|
print(f" errmsg: {token_data.get('errmsg')}")
|
||
|
|
|
||
|
|
if 'access_token' not in token_data:
|
||
|
|
print(" FAILED: No access token returned")
|
||
|
|
exit(1)
|
||
|
|
|
||
|
|
token = token_data['access_token']
|
||
|
|
print(f" access_token: {token[:30]}...")
|
||
|
|
|
||
|
|
# Step 2: Get department list
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("Step 2: Get department list")
|
||
|
|
resp2 = httpx.get(f"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={token}", timeout=10)
|
||
|
|
dept_data = resp2.json()
|
||
|
|
print(f" errcode: {dept_data.get('errcode')}")
|
||
|
|
print(f" errmsg: {dept_data.get('errmsg')}")
|
||
|
|
departments = dept_data.get('department', [])
|
||
|
|
print(f" department count: {len(departments)}")
|
||
|
|
if departments:
|
||
|
|
print(f" first 5 departments:")
|
||
|
|
for d in departments[:5]:
|
||
|
|
print(f" - id={d.get('id')}, name={d.get('name')}, parentid={d.get('parentid')}")
|
||
|
|
|
||
|
|
# Step 3: Get members of root department (id=1)
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("Step 3: Get members of root department (id=1)")
|
||
|
|
resp3 = httpx.get(f"https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={token}&department_id=1&fetch_child=1", timeout=15)
|
||
|
|
user_data = resp3.json()
|
||
|
|
print(f" errcode: {user_data.get('errcode')}")
|
||
|
|
print(f" errmsg: {user_data.get('errmsg')}")
|
||
|
|
users = user_data.get('userlist', [])
|
||
|
|
print(f" user count: {len(users)}")
|
||
|
|
if users:
|
||
|
|
print(f" first 3 users:")
|
||
|
|
for u in users[:3]:
|
||
|
|
print(f" - userid={u.get('userid')}, name={u.get('name')}, department={u.get('department')}")
|
||
|
|
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("SUMMARY:")
|
||
|
|
print(f" Token: OK")
|
||
|
|
print(f" Departments: {len(departments)}")
|
||
|
|
print(f" Users: {len(users)}")
|
||
|
|
if dept_data.get('errcode') == 0 and len(departments) > 0:
|
||
|
|
print(" RESULT: SUCCESS - Contact sync is working!")
|
||
|
|
else:
|
||
|
|
print(" RESULT: STILL FAILING")
|