ASI / env_auth.py
muzsang's picture
Upload 34 files
3069ef4 verified
Raw History Blame Contribute Delete
1.33 kB
#!/usr/bin/env python3
"""
从环境变量中读取认证信息并创建认证文件
"""
import os
import json
import base64
import sys
from pathlib import Path
def setup_auth_from_env():
"""从环境变量AISTUDIO_AUTH_JSON中读取认证信息并创建认证文件"""
print("检查环境变量认证...")
# 检查环境变量是否存在
auth_json_b64 = os.environ.get('AISTUDIO_AUTH_JSON')
if not auth_json_b64:
print("❌ 未找到环境变量 AISTUDIO_AUTH_JSON")
return False
try:
# 从Base64解码认证JSON
auth_json_str = base64.b64decode(auth_json_b64).decode('utf-8')
auth_data = json.loads(auth_json_str)
# 确保认证目录存在
auth_dir = Path(os.environ.get('HOME', '/app')) / 'auth_profiles' / 'active'
auth_dir.mkdir(parents=True, exist_ok=True)
# 创建认证文件
auth_file_path = auth_dir / 'auth_state_env.json'
with open(auth_file_path, 'w') as f:
json.dump(auth_data, f)
print(f"✅ 已成功从环境变量创建认证文件: {auth_file_path}")
return True
except Exception as e:
print(f"❌ 处理环境变量认证时出错: {e}")
return False
if __name__ == "__main__":
setup_auth_from_env()