excel-toolkit/scripts/test_auth.py

76 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试 OpenClaw Auth Runtime
"""
import sys
from pathlib import Path
# 添加当前目录到路径
sys.path.insert(0, str(Path(__file__).parent))
from auth_runtime import (
create_env_config,
get_access_token,
request_api_with_auto_refresh,
load_client_key_from_env,
)
def main():
print("=" * 60)
print("OpenClaw Auth Runtime 测试")
print("=" * 60)
# 方式 1: 从 .env 加载 CLIENT_KEY
try:
client_key = load_client_key_from_env()
print(f"✓ 从 .env 文件加载 CLIENT_KEY: {client_key[:10]}...")
except ValueError as e:
print(f"{e}")
print("\n请在 .env 文件中设置 CLIENT_KEY")
print("或设置环境变量export CLIENT_KEY=your-key")
return 1
# 创建配置
config = create_env_config()
print(f"✓ 配置创建成功")
print(f" AUTH_BASE: {config.auth_base}")
print(f" AUTH_CACHE_DIR: {config.auth_cache_dir}")
# 获取访问令牌
print("\n【测试 1】获取访问令牌...")
try:
token = get_access_token(dry_run=False, config=config)
print(f"✓ 获取成功:{token[:20]}...")
except Exception as e:
print(f"❌ 失败:{e}")
return 1
# 测试 API 请求
print("\n【测试 2】测试 API 请求...")
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},
)
print(f"✓ 响应状态:{response.status}")
print(f" 响应体:{response.body[:100]}...")
except Exception as e:
print(f"❌ 失败:{e}")
print("\n" + "=" * 60)
print("✅ 测试完成")
print("=" * 60)
return 0
if __name__ == "__main__":
sys.exit(main())