47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
生成 KlingAI JWT 认证 Token
|
|
使用环境变量中的 KLINGAI_ACCESS_KEY 和 KLINGAI_SECRET_KEY
|
|
"""
|
|
|
|
import os
|
|
import time
|
|
|
|
from dotenv import load_dotenv
|
|
from jose import jwt
|
|
|
|
# 加载 .env 文件
|
|
load_dotenv()
|
|
|
|
access_key = os.getenv("KLINGAI_ACCESS_KEY", "")
|
|
secret_key = os.getenv("KLINGAI_SECRET_KEY", "")
|
|
|
|
if not access_key or not secret_key:
|
|
print("错误: 请在 .env 文件中配置 KLINGAI_ACCESS_KEY 和 KLINGAI_SECRET_KEY")
|
|
exit(1)
|
|
|
|
# JWT 生成算法与代码中一致
|
|
headers = {"alg": "HS256", "typ": "JWT"}
|
|
current_time = int(time.time())
|
|
payload = {
|
|
"iss": access_key,
|
|
"exp": current_time + 1800, # 30分钟过期
|
|
"nbf": current_time - 5, # 5秒前生效
|
|
}
|
|
|
|
token = jwt.encode(payload, secret_key, algorithm="HS256", headers=headers)
|
|
|
|
print("=" * 60)
|
|
print("KlingAI JWT Token 生成成功")
|
|
print("=" * 60)
|
|
print(f"Access Key: {access_key}")
|
|
print(f"生成时间: {current_time}")
|
|
print(f"过期时间: {current_time + 1800} (30分钟后)")
|
|
print()
|
|
print("Token:")
|
|
print(token)
|
|
print()
|
|
print("使用方式:")
|
|
print(f"Authorization: Bearer {token}")
|
|
print("=" * 60)
|