fix(business): follow Kingdee token request body

Align the generated Kingdee token request with the official enhanced-token document: client_id, client_secret, username, accountId, language, automatic nonce, and automatic current timestamp. Treat expires_in as milliseconds by default and update docs/tests accordingly.

Validation: python3 -m unittest discover -v; python3 -m compileall obsidian_wiki tests; git diff --check.
This commit is contained in:
Codex
2026-07-06 20:11:01 +00:00
parent 4e34e5b3fe
commit 72c666f165
4 changed files with 108 additions and 94 deletions
+19 -32
View File
@@ -67,9 +67,10 @@ class KingdeeApiTests(unittest.TestCase):
self.assertIn("用途:销售订单 API 要求的 x-acgw-identity 请求头。", text)
self.assertIn("是否必填:可选,手动测试用;长期定时同步建议使用自动登录配置。", text)
self.assertIn("是否必填:可选,默认从 data.access_token 读取。", text)
self.assertIn("变量:KINGDEE_APP_KEY", text)
self.assertIn("变量:KINGDEE_TOKEN_TIMESTAMP_FIELD", text)
self.assertIn("支持占位符:{now_ms}", text)
self.assertIn("变量:KINGDEE_CLIENT_ID", text)
self.assertIn("变量:KINGDEE_USERNAME", text)
self.assertIn("变量:KINGDEE_ACCOUNT_ID", text)
self.assertIn("nonce 和 timestamp 会按官方文档自动生成", text)
def test_default_config_path_accepts_none(self):
cwd = Path.cwd()
@@ -109,7 +110,7 @@ class KingdeeApiTests(unittest.TestCase):
{
"data": {
"access_token": "access-1",
"expires_in": 3600,
"expires_in": 3600000,
"refresh_token": "refresh-1",
}
}
@@ -129,44 +130,30 @@ class KingdeeApiTests(unittest.TestCase):
self.assertEqual(request.get_header("X-app"), "demo")
self.assertEqual(stat.S_IMODE(cache_path.stat().st_mode), 0o600)
def test_access_token_body_can_be_built_from_split_fields_with_current_time(self):
def test_access_token_body_defaults_to_official_fields_with_nonce_and_current_time(self):
cache_path = self.root / ".runtime" / "kingdee-token.json"
config = {
"KINGDEE_TOKEN_URL": "https://token.example/access",
"KINGDEE_APP_KEY": "abc",
"KINGDEE_APP_SECRET": "secret",
"KINGDEE_TOKEN_APP_KEY_FIELD": "client_id",
"KINGDEE_TOKEN_APP_SECRET_FIELD": "client_secret",
"KINGDEE_TOKEN_TIMESTAMP_FIELD": "timestamp",
"KINGDEE_TOKEN_TIMESTAMP_FORMAT": "epoch_ms",
"KINGDEE_TOKEN_BODY_EXTRA_JSON": '{"tenant":"demo"}',
"KINGDEE_CLIENT_ID": "thirdapp",
"KINGDEE_CLIENT_SECRET": "secret",
"KINGDEE_USERNAME": "zhangSan",
"KINGDEE_ACCOUNT_ID": "1355633519610561531",
"KINGDEE_LANGUAGE": "zh_CN",
"KINGDEE_TOKEN_FIELD": "data.access_token",
}
http = RecordingUrlopen([{"data": {"access_token": "access-1"}}])
token = get_kingdee_access_token(config, cache_path=cache_path, urlopen=http, now_epoch=1000.5)
token = get_kingdee_access_token(config, cache_path=cache_path, urlopen=http, now_epoch=1000)
self.assertEqual(token, "access-1")
body = json.loads(http.calls[0]["request"].data.decode("utf-8"))
self.assertEqual(body["client_id"], "abc")
self.assertEqual(body["client_id"], "thirdapp")
self.assertEqual(body["client_secret"], "secret")
self.assertEqual(body["timestamp"], 1000500)
self.assertEqual(body["tenant"], "demo")
def test_token_body_json_supports_current_time_placeholders(self):
cache_path = self.root / ".runtime" / "kingdee-token.json"
config = {
"KINGDEE_TOKEN_URL": "https://token.example/access",
"KINGDEE_TOKEN_BODY_JSON": '{"app_key":"abc","timestamp":{now_ms},"requested_at":"{now_datetime_utc}"}',
"KINGDEE_TOKEN_FIELD": "data.access_token",
}
http = RecordingUrlopen([{"data": {"access_token": "access-1"}}])
get_kingdee_access_token(config, cache_path=cache_path, urlopen=http, now_epoch=1000)
body = json.loads(http.calls[0]["request"].data.decode("utf-8"))
self.assertEqual(body["timestamp"], 1000000)
self.assertEqual(body["requested_at"], "1970-01-01 00:16:40")
self.assertEqual(body["username"], "zhangSan")
self.assertEqual(body["accountId"], "1355633519610561531")
self.assertEqual(body["language"], "zh_CN")
self.assertRegex(body["nonce"], r"^[0-9a-f]{32}$")
self.assertEqual(body["timestamp"], "1970-01-01T00:16:40.000Z")
def test_expired_token_uses_refresh_endpoint(self):
cache_path = self.root / ".runtime" / "kingdee-token.json"
@@ -196,7 +183,7 @@ class KingdeeApiTests(unittest.TestCase):
{
"data": {
"access_token": "new-token",
"expires_in": 3600,
"expires_in": 3600000,
"refresh_token": "refresh-2",
}
}