164 lines
3.7 KiB
Markdown
164 lines
3.7 KiB
Markdown
# @clawd/auth-runtime-py
|
||
|
||
Python 版本的 OpenClaw Auth Runtime,基于 `~/clawd/skills/_shared/auth-runtime` 的 TypeScript 实现。
|
||
|
||
## 安装
|
||
|
||
### 方式 1: 作为本地包安装(推荐)
|
||
|
||
```bash
|
||
# 在具体 skill 的目录中
|
||
uv pip install /path/to/python_auth_runtime
|
||
```
|
||
|
||
### 方式 2: 作为文件依赖
|
||
|
||
在 `pyproject.toml` 中添加:
|
||
|
||
```toml
|
||
[project]
|
||
dependencies = [
|
||
"python-auth-runtime-py @ file:///path/to/python_auth_runtime",
|
||
]
|
||
```
|
||
|
||
### 方式 3: 复制源码
|
||
|
||
直接复制 `src/python_auth_runtime` 到你的项目中:
|
||
|
||
```bash
|
||
cp -r /path/to/python_auth_runtime/src/python_auth_runtime ./your-skill/
|
||
```
|
||
|
||
## 使用方式
|
||
|
||
### 1. 设置环境变量
|
||
|
||
```bash
|
||
export CLIENT_KEY="your-client-key"
|
||
export AUTH_BASE="https://api-gw-test.yuanwei-lnc.com" # 可选
|
||
```
|
||
|
||
### 2. 导入并使用
|
||
|
||
```python
|
||
from python_auth_runtime import create_env_config, get_access_token, request_api_with_auto_refresh
|
||
|
||
# 创建配置(自动从环境变量加载)
|
||
config = create_env_config()
|
||
|
||
# 获取访问令牌(带缓存)
|
||
token = get_access_token(dry_run=False, config=config)
|
||
|
||
# 发送 API 请求(自动处理令牌刷新)
|
||
response = request_api_with_auto_refresh(
|
||
method="POST",
|
||
url=f"{config.auth_base}/ecom/tasks/scrape",
|
||
dry_run=False,
|
||
config=config,
|
||
body={"url": "https://detail.1688.com/offer/123.html"},
|
||
)
|
||
|
||
if response.status == 200:
|
||
import json
|
||
data = json.loads(response.body)
|
||
print("商品价格:", data["price"])
|
||
```
|
||
|
||
## API 参考
|
||
|
||
### 配置
|
||
|
||
#### `create_env_config() -> EnvConfig`
|
||
|
||
从环境变量创建配置:
|
||
|
||
| 环境变量 | 默认值 | 说明 |
|
||
|---------|--------|------|
|
||
| `AUTH_BASE` | `https://api-gw-test.yuanwei-lnc.com` | 认证基础 URL |
|
||
| `CLIENT_KEY` | **必需** | 客户端密钥 |
|
||
| `AUTH_CACHE_DIR` | `/tmp/skill-auth-cache` | 缓存目录 |
|
||
| `AUTH_MIN_TTL_SEC` | `60` | 最小令牌 TTL(秒) |
|
||
|
||
### 令牌管理
|
||
|
||
#### `get_access_token(dry_run, config) -> str`
|
||
|
||
获取访问令牌(带缓存)
|
||
|
||
#### `refresh_access_token(dry_run, config) -> str`
|
||
|
||
刷新访问令牌(绕过缓存)
|
||
|
||
### API 请求
|
||
|
||
#### `request_api(method, url, auth_token, body) -> ApiResponse`
|
||
|
||
发送 HTTP 请求
|
||
|
||
#### `request_api_with_auto_refresh(method, url, dry_run, config, body) -> ApiResponse`
|
||
|
||
发送 API 请求并自动刷新令牌
|
||
|
||
## 与 TypeScript 版本对比
|
||
|
||
| 特性 | TypeScript | Python |
|
||
|------|-----------|--------|
|
||
| 包名 | `@clawd/auth-runtime` | `@clawd/auth-runtime-py` |
|
||
| 安装方式 | `bun add file:../_shared/auth-runtime` | `uv pip install /path/to/python_auth_runtime` |
|
||
| 导入 | `import { ... } from '@clawd/auth-runtime'` | `from python_auth_runtime import ...` |
|
||
| HTTP 客户端 | `fetch()` | `requests` |
|
||
|
||
## 示例项目
|
||
|
||
```python
|
||
# your-skill/main.py
|
||
from python_auth_runtime import create_env_config, request_api_with_auto_refresh
|
||
import os
|
||
|
||
def main():
|
||
# 从环境变量加载 CLIENT_KEY
|
||
config = create_env_config()
|
||
|
||
# 调用 1688 API
|
||
response = request_api_with_auto_refresh(
|
||
method="POST",
|
||
url=f"{config.auth_base}/ecom/tasks/scrape",
|
||
dry_run=False,
|
||
config=config,
|
||
body={"url": "https://detail.1688.com/offer/123.html"},
|
||
)
|
||
|
||
if response.status == 200:
|
||
print("成功:", response.body)
|
||
else:
|
||
print("失败:", response.status, response.body)
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
```
|
||
|
||
## 环境变量配置
|
||
|
||
在项目根目录创建 `.env` 文件(不提交到 git):
|
||
|
||
```bash
|
||
# .env
|
||
CLIENT_KEY=your-client-key-here
|
||
AUTH_BASE=https://api-gw-test.yuanwei-lnc.com
|
||
```
|
||
|
||
然后在代码中加载:
|
||
|
||
```python
|
||
from dotenv import load_dotenv
|
||
load_dotenv() # 加载 .env 文件到环境变量
|
||
|
||
from python_auth_runtime import create_env_config
|
||
config = create_env_config()
|
||
```
|
||
|
||
## 许可证
|
||
|
||
MIT
|