Sign inSign up

spf20081/gemini-web2api

By spf20081

•Updated 7 months ago

Image
0

399

spf20081/gemini-web2api repository overview

⁠Gemini-Web2API 部署文档

将 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-1PSIDGoogle Cookie(主账号)✅
__Secure-1PSIDTSGoogle Cookie(主账号)✅
PROXY_API_KEYAPI 鉴权密钥,调用时携带✅
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 有效期约数天至数周,过期后需重新获取。

⁠方法一:浏览器控制台(最快)
  1. 打开 gemini.google.com⁠ 并确认已登录
  2. 按 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));
  1. 将复制的内容粘贴到 .env 对应字段
⁠方法二:Cookie Editor 扩展
  1. 安装 Cookie Editor⁠
  2. 打开 gemini.google.com → 点击扩展图标
  3. 搜索并复制 __Secure-1PSID / __Secure-1PSIDTS 的值
⁠方法三:程序自动读取本机 Chrome(Windows)
# 关闭 Chrome 后执行
.\Gemini-Web2API.exe --fetch-cookies

⁠API 使用

服务地址:http://localhost:8007
鉴权方式:Authorization: Bearer <PROXY_API_KEY>

⁠OpenAI 兼容端点
# 获取模型列表
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"
  }'
⁠Claude 兼容端点
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

⁠Docker Hub

镜像地址:hub.docker.com/r/spf20081/gemini-web2api⁠

docker pull spf20081/gemini-web2api:latest

Tag summary

Content type

Image

Digest

sha256:031c58576…

Size

23.8 MB

Last updated

7 months ago

docker pull spf20081/gemini-web2api