将 Google Gemini 网页版转换为 OpenAI / Claude 兼容 API。
前提:已安装 Docker Desktop
# 1. 创建工作目录
mkdir gemini-web2api && cd gemini-web2api
# 2. 创建 .env 配置文件(填入 Cookie 后再启动)
curl -o .env https://raw.githubusercontent.com/spfnas/Gemini-Web2API/main/.env.example
# 3. 创建 docker-compose.yml
docker-compose.yml 内容:
services:
gemini-web2api:
image: spf20081/gemini-web2api:latest
container_name: gemini-web2api
ports:
- "8007:8007"
volumes:
- ./.env:/app/.env:ro
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:8007/"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
# 4. 填好 .env 后启动
docker-compose up -d
git clone https://github.com/spfnas/Gemini-Web2API.git
cd Gemini-Web2API
cp .env.example .env # 填写 Cookie
docker-compose up -d --build
编辑 .env 文件:
| 变量 | 说明 | 必填 |
|---|---|---|
__Secure-1PSID | Google Cookie(主账号) | ✅ |
__Secure-1PSIDTS | Google Cookie(主账号) | ✅ |
PROXY_API_KEY | API 鉴权密钥,调用时携带 | ✅ |
PORT | 监听端口,默认 8007 | ❌ |
LANGUAGE | 请求语言,默认 en,可填 zh-CN | ❌ |
MODEL_MAPPING | 模型名映射,格式 source:target | ❌ |
ACCOUNTS | 多账号列表,留空自动使用所有 | ❌ |
多账号配置示例:
__Secure-1PSID_work=xxx
__Secure-1PSIDTS_work=yyy
__Secure-1PSID_home=xxx
__Secure-1PSIDTS_home=yyy
ACCOUNTS=work,home
Google Cookie 有效期约数天至数周,过期后需重新获取。
F12 → Console 标签,粘贴以下代码并回车:const keys = ['__Secure-1PSID', '__Secure-1PSIDTS'];
const result = {};
document.cookie.split(';').forEach(c => {
const [k, v] = c.trim().split('=');
if (keys.includes(k)) result[k] = v;
});
const text = `__Secure-1PSID=${result['__Secure-1PSID']}\n__Secure-1PSIDTS=${result['__Secure-1PSIDTS']}`;
navigator.clipboard.writeText(text).then(() => console.log('✅ 已复制到剪贴板!\n' + text));
.env 对应字段__Secure-1PSID / __Secure-1PSIDTS 的值# 关闭 Chrome 后执行
.\Gemini-Web2API.exe --fetch-cookies
服务地址:http://localhost:8007
鉴权方式:Authorization: Bearer <PROXY_API_KEY>
# 获取模型列表
curl http://localhost:8007/v1/models \
-H "Authorization: Bearer changeme"
# 聊天
curl http://localhost:8007/v1/chat/completions \
-H "Authorization: Bearer changeme" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [{"role": "user", "content": "你好"}],
"stream": false
}'
# 图片生成
curl http://localhost:8007/v1/images/generations \
-H "Authorization: Bearer changeme" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-2.5-flash-image",
"prompt": "a cat wearing a hat",
"n": 1,
"size": "1024x1024",
"response_format": "b64_json"
}'
curl http://localhost:8007/v1/messages \
-H "Authorization: Bearer changeme" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-haiku-4-5-20251001",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'
| 模型 ID | 说明 |
|---|---|
gemini-2.5-flash | 快速通用模型 |
gemini-3.1-pro-preview | 高性能模型 |
gemini-3-flash-preview | 最新 Flash 模型 |
gemini-3-flash-preview-no-thinking | 无思考过程版本 |
gemini-2.5-flash-image | 图片生成 |
gemini-3-pro-image-preview | 高质量图片生成 |
# 启动服务
docker-compose up -d
# 停止服务
docker-compose down
# 查看实时日志
docker-compose logs -f
# 重启服务(更新 Cookie 后)
docker-compose restart
# 强制重新拉取最新镜像
docker-compose pull && docker-compose up -d --force-recreate
当日志出现以下提示时,说明 Cookie 已失效:
Account 'default': init failed ... SNlM0e token not found. Cookies might be invalid
处理步骤:
# 1. 重新获取 Cookie(见上方"Cookie 获取方法")
# 2. 更新 .env 文件中的两个字段
# 3. 重启容器
docker-compose restart
镜像地址:hub.docker.com/r/spf20081/gemini-web2api
docker pull spf20081/gemini-web2api:latest
Content type
Image
Digest
sha256:031c58576…
Size
23.8 MB
Last updated
7 months ago
docker pull spf20081/gemini-web2api