| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- 飞书凭证读取(四个脚本共用)。
- 按顺序取第一个可用来源:
- 1) 环境变量 FEISHU_APP_ID / FEISHU_APP_SECRET
- 2) ~/.sku_audit/credentials.json
- 3) 技能包根目录的 credentials.json(托管沙箱无法配置环境变量或用户目录时随包提供)
- 文件格式:{"app_id": "...", "app_secret": "..."}
- 凭证值不写入任何输出;提示信息只包含路径。
- """
- import json
- import os
- FILE_NAME = "credentials.json"
- HOME_FILE = os.path.join(os.path.expanduser("~/.sku_audit"), FILE_NAME)
- PACKAGE_FILE = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), FILE_NAME)
- def read_file(path):
- """读取一个凭证文件;不存在、无法解析或缺项时返回 None。"""
- try:
- with open(path, encoding="utf-8-sig") as f:
- data = json.load(f)
- except (OSError, ValueError):
- return None
- if not isinstance(data, dict):
- return None
- app_id, secret = data.get("app_id"), data.get("app_secret")
- if isinstance(app_id, str) and isinstance(secret, str) and app_id.strip() and secret.strip():
- return app_id.strip(), secret.strip()
- return None
- def load():
- """返回 (app_id, app_secret);没有可用来源时返回 None。"""
- app_id, secret = os.environ.get("FEISHU_APP_ID"), os.environ.get("FEISHU_APP_SECRET")
- if app_id and secret:
- return app_id, secret
- for path in (HOME_FILE, PACKAGE_FILE):
- found = read_file(path)
- if found:
- return found
- return None
- def missing_message():
- return ("未找到可用凭证。请设置环境变量 FEISHU_APP_ID / FEISHU_APP_SECRET,"
- f"或写入 {HOME_FILE},或在技能包根目录放置 {FILE_NAME}")
|