# 北京大学账号验证 用于验证某个学号/职工号是否为有效的北京大学账号,不涉及登录流程。 ## API ``` GET https://iaaa.pku.edu.cn/iaaa/svc/pub/validate.do ?userId={USERID} &userName={USERNAME} &appId={APPID} &msgAbs={MD5MSG} ``` ## 参数 | 参数 | 说明 | |------|------| | `userId` | 学号或职工号 | | `userName` | 账号姓名(传输时需 URLEncode,但计算 MD5 摘要时使用原始值,不做 URLEncode) | | `appId` | 应用 ID | | `msgAbs` | MD5 消息摘要 | ## msgAbs 计算 与其他端点规则一致:除 `msgAbs` 外所有参数按参数名升序排列拼接,再拼接 Key,取 MD5。 PARA_STR 排序后为:`appId={APPID}&userId={USERID}&userName={USERNAME}` 注意:`userName` 在 PARA_STR 中使用**原始值**(不做 URLEncode),只有在 URL 传输时才 URLEncode。 ## 返回 **正常:** ```json { "success": true, "valid": true, "userType": "用户身份类别" } ``` `valid` 为 `true` 表示账号存在,`false` 表示不存在。 **异常:** ```json { "success": false, "errCode": "错误代码", "errMsg": "错误信息" } ``` ## Python 示例 ```python import hashlib import urllib.parse import requests def verify_pku_account(user_id: str, user_name: str, app_id: str, key: str) -> dict: """验证北大账号是否存在""" # msgAbs 中 userName 使用原始值 para_str = f"appId={app_id}&userId={user_id}&userName={user_name}" msg_abs = hashlib.md5((para_str + key).encode('utf-8')).hexdigest() # URL 中 userName 需要 URLEncode url = ( f"https://iaaa.pku.edu.cn/iaaa/svc/pub/validate.do" f"?appId={app_id}" f"&userId={user_id}" f"&userName={urllib.parse.quote(user_name)}" f"&msgAbs={msg_abs}" ) resp = requests.get(url, timeout=10) resp.raise_for_status() return resp.json() ```