excel-toolkit/python_auth_runtime/scripts/example_usage.py

55 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
示例如何在具体 skill 中使用 auth_runtime
参考~/clawd/skills/1688-product-master/scripts/run.ts
"""
import sys
from pathlib import Path
# 添加当前目录到路径
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
# 步骤 1: 加载 .env.local
from load_env import load_env
load_env() # 加载 .env.local 或 .env
# 步骤 2: 导入 auth_runtime
from python_auth_runtime import create_env_config, request_api_with_auto_refresh
def main():
print("=" * 60)
print("Auth Runtime 使用示例")
print("=" * 60)
# 步骤 3: 创建配置(从环境变量读取)
config = create_env_config()
print(f"\n✓ 配置创建成功")
print(f" AUTH_BASE: {config.auth_base}")
print(f" CLIENT_KEY: {config.client_key[:10]}..." if config.client_key else " CLIENT_KEY: <empty>")
# 步骤 4: 调用 API
print(f"\n【测试】获取访问令牌...")
try:
response = request_api_with_auto_refresh(
method="POST",
url=f"{config.auth_base}/auth/skill-credit/session",
dry_run=False,
config=config,
body={"clientKey": config.client_key} if config.client_key else {},
)
print(f"✓ 响应状态:{response.status}")
if response.status == 200:
print(f" 响应体:{response.body[:100]}...")
else:
print(f" 响应体:{response.body}")
except Exception as e:
print(f"❌ 失败:{e}")
print("\n" + "=" * 60)
if __name__ == "__main__":
main()