Skip to content

Commit abbfdea

Browse files
committed
删除GOOGLE_CREDENTIALS_1冗余代码
1 parent c084b38 commit abbfdea

3 files changed

Lines changed: 22 additions & 96 deletions

File tree

.env.example

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ CREDENTIALS_DIR=./creds
4949
# 默认: false
5050
AUTO_LOAD_ENV_CREDS=false
5151

52-
# Google 凭证 JSON (可选,可以直接通过环境变量提供凭证)
53-
# 支持 base64 编码或直接 JSON 字符串
54-
# GOOGLE_CREDENTIALS={"type":"authorized_user","client_id":"...","refresh_token":"..."}
55-
# GOOGLE_CREDENTIALS_2={"type":"authorized_user","client_id":"...","refresh_token":"..."}
56-
# ... 最多支持 GOOGLE_CREDENTIALS_10
52+
# Google 凭证环境变量配置 (可选,通过 GCLI_CREDS_* 环境变量提供凭证)
53+
# 支持编号格式和项目名格式
54+
# GCLI_CREDS_1={"client_id":"your-client-id","client_secret":"your-secret","refresh_token":"your-token","token_uri":"https://oauth2.googleapis.com/token","project_id":"your-project"}
55+
# GCLI_CREDS_2={"client_id":"...","project_id":"..."}
56+
# GCLI_CREDS_myproject={"client_id":"...","project_id":"myproject",...}
5757

5858
# ================================================================
5959
# 凭证轮换配置

README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,29 +187,26 @@ docker run -d --name gcli2api --network host -e API_PASSWORD=api_pwd -e PANEL_PA
187187
- `PASSWORD`: 通用密码,设置后覆盖上述两个(默认:pwd)
188188

189189
**凭证配置**
190-
- `GOOGLE_CREDENTIALS`: Google OAuth 凭证 JSON(支持原始 JSON 或 base64 编码)
191-
- `GOOGLE_CREDENTIALS_2` 到 `GOOGLE_CREDENTIALS_10`: 额外的凭证(用于多凭证轮换)
190+
191+
支持使用 `GCLI_CREDS_*` 环境变量导入多个凭证:
192192

193193
#### 凭证环境变量使用示例
194194

195-
**方式 1:直接传入 JSON**
195+
**方式 1:编号格式**
196196
```bash
197-
export GOOGLE_CREDENTIALS='{"type":"authorized_user","client_id":"...","client_secret":"...","refresh_token":"..."}'
197+
export GCLI_CREDS_1='{"client_id":"your-client-id","client_secret":"your-secret","refresh_token":"your-token","token_uri":"https://oauth2.googleapis.com/token","project_id":"your-project"}'
198+
export GCLI_CREDS_2='{"client_id":"...","project_id":"..."}'
198199
```
199200

200-
**方式 2:Base64 编码(推荐,更安全)**
201+
**方式 2:项目名格式**
201202
```bash
202-
# 将凭证文件转为 base64
203-
cat credential.json | base64 -w 0 > credential.b64
204-
# 设置环境变量
205-
export GOOGLE_CREDENTIALS=$(cat credential.b64)
203+
export GCLI_CREDS_myproject='{"client_id":"...","project_id":"myproject",...}'
204+
export GCLI_CREDS_project2='{"client_id":"...","project_id":"project2",...}'
206205
```
207206

208-
**方式 3:多凭证轮换**
207+
**启用自动加载**
209208
```bash
210-
export GOOGLE_CREDENTIALS='{"type":"authorized_user",...}' # 第一个凭证
211-
export GOOGLE_CREDENTIALS_2='{"type":"authorized_user",...}' # 第二个凭证
212-
export GOOGLE_CREDENTIALS_3='{"type":"authorized_user",...}' # 第三个凭证
209+
export AUTO_LOAD_ENV_CREDS=true # 程序启动时自动导入环境变量凭证
213210
```
214211

215212
**Docker 使用示例**

src/credential_manager.py

Lines changed: 7 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -372,59 +372,14 @@ async def _discover_credential_files(self):
372372
old_files = set(self._credential_files)
373373
all_files = []
374374

375-
# First, check environment variables for credentials
376-
env_creds_loaded = False
377-
for i in range(1, 11): # Support up to 10 credentials from env vars
378-
env_var_name = f"GOOGLE_CREDENTIALS_{i}" if i > 1 else "GOOGLE_CREDENTIALS"
379-
env_creds = os.getenv(env_var_name)
380-
381-
if env_creds:
382-
try:
383-
# Try to decode if it's base64 encoded
384-
try:
385-
# Check if it's base64 encoded
386-
decoded = base64.b64decode(env_creds)
387-
env_creds = decoded.decode('utf-8')
388-
log.debug(f"Decoded base64 credential from {env_var_name}")
389-
except:
390-
# Not base64, use as is
391-
pass
392-
393-
# Parse the JSON credential from environment variable
394-
cred_data = json.loads(env_creds)
395-
396-
# Auto-add 'type' field if missing but has required OAuth fields
397-
if 'type' not in cred_data and all(key in cred_data for key in ['client_id', 'refresh_token']):
398-
cred_data['type'] = 'authorized_user'
399-
log.debug(f"Auto-added 'type' field to credential from {env_var_name}")
400-
401-
if all(key in cred_data for key in ['type', 'client_id', 'refresh_token']):
402-
# Save to a temporary file for compatibility with existing code
403-
temp_file = os.path.join(CREDENTIALS_DIR, f"env_credential_{i}.json")
404-
os.makedirs(CREDENTIALS_DIR, exist_ok=True)
405-
with open(temp_file, 'w') as f:
406-
json.dump(cred_data, f)
407-
all_files.append(temp_file)
408-
log.info(f"Loaded credential from environment variable: {env_var_name}")
409-
env_creds_loaded = True
410-
else:
411-
log.warning(f"Invalid credential format in {env_var_name}")
412-
except json.JSONDecodeError as e:
413-
log.warning(f"Failed to parse JSON from {env_var_name}: {e}")
414-
except Exception as e:
415-
log.warning(f"Error loading credential from {env_var_name}: {e}")
375+
# Discover from directory
376+
credentials_dir = CREDENTIALS_DIR
377+
patterns = [os.path.join(credentials_dir, "*.json")]
416378

417-
# If no env credentials, discover from directory
418-
if not env_creds_loaded:
419-
credentials_dir = CREDENTIALS_DIR
420-
patterns = [os.path.join(credentials_dir, "*.json")]
421-
422-
for pattern in patterns:
423-
discovered_files = glob.glob(pattern)
424-
# Skip env_credential files if loading from directory
425-
for file in discovered_files:
426-
if not os.path.basename(file).startswith("env_credential_"):
427-
all_files.append(file)
379+
for pattern in patterns:
380+
discovered_files = glob.glob(pattern)
381+
for file in discovered_files:
382+
all_files.append(file)
428383

429384
all_files = sorted(list(set(all_files)))
430385

@@ -547,32 +502,6 @@ async def _discover_credential_files_unlocked(self):
547502
from config import CREDENTIALS_DIR
548503
import glob
549504

550-
# 检查环境变量凭证(与 _discover_credential_files 保持一致)
551-
for i in range(1, 11):
552-
env_var_name = f"GOOGLE_CREDENTIALS_{i}" if i > 1 else "GOOGLE_CREDENTIALS"
553-
env_creds = os.getenv(env_var_name)
554-
555-
if env_creds:
556-
try:
557-
# 检查是否为base64编码
558-
try:
559-
import base64
560-
decoded = base64.b64decode(env_creds)
561-
env_creds = decoded.decode('utf-8')
562-
except:
563-
pass
564-
565-
# 验证JSON格式
566-
import json
567-
json.loads(env_creds)
568-
569-
# 创建临时文件路径标识
570-
temp_env_path = f"<ENV_{env_var_name}>"
571-
if not self.is_cred_disabled(temp_env_path):
572-
temp_files.append(temp_env_path)
573-
574-
except (json.JSONDecodeError, UnicodeDecodeError) as e:
575-
log.error(f"Invalid JSON in {env_var_name}: {e}")
576505

577506
# 检查文件系统中的凭证
578507
if os.path.exists(CREDENTIALS_DIR):

0 commit comments

Comments
 (0)