Commit ·
a939691
1
Parent(s): 1ac79d3
added authentication and oauth token refresh
Browse filesFiles changed (4)
- .gitignore+3
- README.md+120-9
- gemini_proxy.py+142-40
- requirements.txt+2-1
1 unmodified line2345671 unmodified lineoauth_creds.json*.json# Python__pycache__/*.py[cod]1 unmodified line23456789101 unmodified lineoauth_creds.json*.json# Environment configuration.env# Python__pycache__/*.py[cod]
27 unmodified lines282930313233343536373839404142434445464715 unmodified lines636465666768692 unmodified lines7273747576777879808182838485868788899091929394959697989910010110210310410527 unmodified lines### First Time Setup1. **Start the proxy server:**```bashpython gemini_proxy.py```2. **Authenticate with Google:**- On first run, the proxy will display an authentication URL- Open the URL in your browser and sign in with your Google account- Grant the necessary permissions- The browser will show "Authentication successful!" when complete- The proxy will automatically save your credentials for future use3. **Project ID Detection:**- The proxy will automatically detect and cache your Google Cloud project ID- This only happens once - subsequent runs will use the cached project ID### Regular Usage15 unmodified linesExample request:```bashcurl -X POST http://localhost:8888/v1/models/gemini-pro:generateContent \-H "Content-Type: application/json" \-d '{"contents": [{2 unmodified lines}'```## ConfigurationThe proxy uses the following configuration:- **Port:** 8888 (hardcoded)- **Credential file:** `oauth_creds.json` (automatically created)- **Scopes:** Cloud Platform, User Info (email/profile), OpenID## File Structure- `gemini_proxy.py` - Main proxy server- `oauth_creds.json` - Cached OAuth credentials and project ID (auto-generated)- `requirements.txt` - Python dependencies- `.gitignore` - Prevents credential files from being committed## Troubleshooting### Port Already in UseIf you see "error while attempting to bind on address", another instance is already running. Stop the existing process or use a different port.### Authentication Issues- Delete `oauth_creds.json` and restart to re-authenticate- Ensure your Google account has access to Google Cloud and Gemini API- Check that the required scopes are granted during authentication### Project ID Issues- The proxy automatically detects your project ID on first run- If detection fails, check your Google Cloud project permissions- Delete `oauth_creds.json` to force re-detection## Security Notes27 unmodified lines2829303132333435363738394041424344454647484950515253545556575815 unmodified lines747576777879802 unmodified lines838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521627 unmodified lines### First Time Setup1. **(Optional) Configure your settings:**If you know your Google Cloud project ID, you can set it in the `.env` file to skip automatic detection:```bash# Edit the .env fileGEMINI_PROJECT_ID=your-project-id# Optional: Change the default portGEMINI_PORT=8888```2. **Start the proxy server:**```bashpython gemini_proxy.py```3. **Authenticate with Google:**- On first run, the proxy will display an authentication URL- Open the URL in your browser and sign in with your Google account- Grant the necessary permissions- The browser will show "Authentication successful!" when complete- The proxy will automatically save your credentials for future use4. **Project ID Detection:**- If `GEMINI_PROJECT_ID` is set in the `.env` file, it will be used- Otherwise, the proxy will automatically detect and cache your Google Cloud project ID- This only happens once - subsequent runs will use the cached project ID### Regular Usage15 unmodified linesExample request:```bashcurl -X POST "http://localhost:8888/v1/models/gemini-pro:generateContent?key=123456" \-H "Content-Type: application/json" \-d '{"contents": [{2 unmodified lines}'```**Note:** The proxy supports multiple authentication methods. The `key` query parameter is the most compatible with standard Gemini clients.### Safety SettingsThe proxy automatically sets default safety settings to `BLOCK_NONE` for all categories if no safety settings are specified in the request. This provides maximum flexibility for content generation. The default categories are:- `HARM_CATEGORY_HARASSMENT`- `HARM_CATEGORY_HATE_SPEECH`- `HARM_CATEGORY_SEXUALLY_EXPLICIT`- `HARM_CATEGORY_DANGEROUS_CONTENT`You can override these defaults by including your own `safetySettings` in the request payload.## AuthenticationThe proxy supports multiple authentication methods for maximum compatibility:- **Default Password:** `123456`- **Configuration:** Set `GEMINI_AUTH_PASSWORD` in `.env` file to change the password### Authentication Methods1. **API Key (Query Parameter)** - Compatible with standard Gemini clients:```bashcurl -X POST "http://localhost:8888/v1/models/gemini-pro:generateContent?key=123456" \-H "Content-Type: application/json" \-d '{"contents": [{"parts": [{"text": "Hello!"}]}]}'```2. **Bearer Token** - Standard API token format:```bashcurl -X POST http://localhost:8888/v1/models/gemini-pro:generateContent \-H "Authorization: Bearer 123456" \-H "Content-Type: application/json" \-d '{"contents": [{"parts": [{"text": "Hello!"}]}]}'```3. **HTTP Basic Authentication** - Traditional username/password:```bashcurl -u "user:123456" -X POST http://localhost:8888/v1/models/gemini-pro:generateContent \-H "Content-Type: application/json" \-d '{"contents": [{"parts": [{"text": "Hello!"}]}]}'```**Python examples:**```pythonimport requestsfrom requests.auth import HTTPBasicAuth# Method 1: Query parameterresponse = requests.post("http://localhost:8888/v1/models/gemini-pro:generateContent?key=123456",json={"contents": [{"parts": [{"text": "Hello!"}]}]})# Method 2: Bearer tokenresponse = requests.post("http://localhost:8888/v1/models/gemini-pro:generateContent",headers={"Authorization": "Bearer 123456"},json={"contents": [{"parts": [{"text": "Hello!"}]}]})# Method 3: Basic authresponse = requests.post("http://localhost:8888/v1/models/gemini-pro:generateContent",auth=HTTPBasicAuth("user", "123456"),json={"contents": [{"parts": [{"text": "Hello!"}]}]})```## ConfigurationThe proxy uses the following configuration:- **Port:** 8888 (default, configurable via `.env`)- **Credential file:** `oauth_creds.json` (automatically created)- **Configuration file:** `.env` (optional settings)- **Scopes:** Cloud Platform, User Info (email/profile), OpenID### Configuration File (.env)You can create a `.env` file in the same directory as the script to configure the proxy:```bash# Set your Google Cloud Project ID to skip automatic detectionGEMINI_PROJECT_ID=my-gcp-project-123# Set a custom port (default is 8888)GEMINI_PORT=9000# Set authentication password (default is 123456)GEMINI_AUTH_PASSWORD=your-secure-password```**Note:** The `.env` file is automatically excluded from version control via `.gitignore`.## File Structure- `gemini_proxy.py` - Main proxy server- `oauth_creds.json` - Cached OAuth credentials and project ID (auto-generated)- `requirements.txt` - Python dependencies- `.env` - Configuration file (optional, create as needed)- `.gitignore` - Prevents credential and config files from being committed## Troubleshooting### Port Already in UseIf you see "error while attempting to bind on address", another instance is already running. Stop the existing process or use a different port.### Authentication Issues (Google OAuth)- Delete `oauth_creds.json` and restart to re-authenticate- Ensure your Google account has access to Google Cloud and Gemini API- Check that the required scopes are granted during authentication### Authentication Issues (Proxy Access)- If you get 401 Unauthorized errors, check your authentication method- Default password is `123456` unless changed in `.env` file- Try different authentication methods:- Query parameter: `?key=123456`- Bearer token: `Authorization: Bearer 123456`- Basic auth: `Authorization: Basic base64(user:123456)`- Most Gemini clients work best with the query parameter method (`?key=password`)### Project ID Issues- The proxy automatically detects your project ID on first run- If detection fails, you can manually set `GEMINI_PROJECT_ID` in the `.env` file- Check your Google Cloud project permissions if auto-detection fails- Delete `oauth_creds.json` to force re-detection- Project ID in `.env` file takes priority over cached and auto-detected project IDs## Security Notes
2 unmodified lines3456789101112131415161718196 unmodified lines26272829303132333435363745 unmodified lines8384858687888990919293946 unmodified lines101102103104105106107108144 unmodified lines25325425525625725825926026126226314 unmodified lines2782792802812822832842852862872883 unmodified lines29229329429529629729819 unmodified lines3183193203213223233243253265 unmodified lines33233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538623 unmodified lines4104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444455 unmodified lines45145245345445545645745845946046146210 unmodified lines4734744754764774784794802 unmodified linesimport requestsimport reimport uvicornfrom datetime import datetimefrom fastapi import FastAPI, Request, Responsefrom fastapi.responses import StreamingResponsefrom http.server import BaseHTTPRequestHandler, HTTPServerfrom urllib.parse import urlparse, parse_qsimport ijsonfrom google.oauth2.credentials import Credentialsfrom google_auth_oauthlib.flow import Flowfrom google.auth.transport.requests import Request as GoogleAuthRequest# --- Configuration ---CLIENT_ID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"CLIENT_SECRET = "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"6 unmodified linesGEMINI_DIR = os.path.dirname(os.path.abspath(__file__)) # Same directory as the scriptCREDENTIAL_FILE = os.path.join(GEMINI_DIR, "oauth_creds.json")CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com"# --- Global State ---credentials = Noneuser_project_id = Noneapp = FastAPI()# Helper class to adapt a generator of bytes into a file-like object# that ijson can read from.45 unmodified linesself.wfile.write(b"<h1>Authentication failed.</h1><p>Please try again.</p>")def get_user_project_id(creds):"""Gets the user's project ID from cache or by probing the API."""global user_project_idif user_project_id:return user_project_id# First, try to load project ID from credential fileif os.path.exists(CREDENTIAL_FILE):try:with open(CREDENTIAL_FILE, "r") as f:6 unmodified linesexcept Exception as e:print(f"Could not load project ID from cache: {e}")# If not found in cache, probe for itprint("Project ID not found in cache. Probing for user project ID...")headers = {"Authorization": f"Bearer {creds.token}","Content-Type": "application/json",144 unmodified lines@app.post("/{full_path:path}")async def proxy_request(request: Request, full_path: str):creds = get_credentials()if not creds:return Response(content="Authentication failed. Please restart the proxy to log in.", status_code=500)proj_id = get_user_project_id(creds)if not proj_id:return Response(content="Failed to get user project ID.", status_code=500)14 unmodified linesis_streaming = Trueelse:target_url = f"{CODE_ASSIST_ENDPOINT}{path}"try:incoming_json = json.loads(post_data)final_model = model_name_from_url if model_match else incoming_json.get("model")structured_payload = {"model": final_model,"project": proj_id,3 unmodified lines"cachedContent": incoming_json.get("cachedContent"),"tools": incoming_json.get("tools"),"toolConfig": incoming_json.get("toolConfig"),"safetySettings": incoming_json.get("safetySettings"),"generationConfig": incoming_json.get("generationConfig"),},}19 unmodified linesprint(f"[STREAM] Starting streaming request to: {target_url}")print(f"[STREAM] Request payload size: {len(final_post_data)} bytes")with requests.post(target_url, data=final_post_data, headers=headers, stream=True) as resp:print(f"[STREAM] Response status: {resp.status_code}")print(f"[STREAM] Response headers: {dict(resp.headers)}")resp.raise_for_status()buffer = ""5 unmodified linesprint(f"[STREAM] Starting to process chunks...")for chunk in resp.iter_content(chunk_size=1024, decode_unicode=True):chunk_count += 1chunk_size = len(chunk) if chunk else 0total_bytes += chunk_sizeprint(f"[STREAM] Chunk #{chunk_count}: {chunk_size} bytes, total: {total_bytes} bytes")if chunk:print(f"[STREAM] Chunk content preview: {repr(chunk[:100])}")buffer += chunkprint(f"[STREAM] Buffer size after chunk: {len(buffer)} chars")# Process complete JSON objects from the bufferprocessing_iterations = 0while buffer:processing_iterations += 1if processing_iterations > 100: # Prevent infinite loopsprint(f"[STREAM] WARNING: Too many processing iterations, breaking")breakbuffer = buffer.lstrip()if not buffer:print(f"[STREAM] Buffer empty after lstrip")breakprint(f"[STREAM] Processing buffer (len={len(buffer)}): {repr(buffer[:50])}")# Handle array startif buffer.startswith('[') and not in_array:print(f"[STREAM] Found array start, entering array mode")buffer = buffer[1:].lstrip()in_array = Truecontinue# Handle array endif buffer.startswith(']'):print(f"[STREAM] Found array end, stopping processing")break# Skip commas between objectsif buffer.startswith(','):print(f"[STREAM] Skipping comma separator")buffer = buffer[1:].lstrip()continue# Look for complete JSON objectsif buffer.startswith('{'):print(f"[STREAM] Found object start, parsing JSON object...")brace_count = 0in_string = Falseescape_next = False23 unmodified linesjson_str = buffer[:end_pos]buffer = buffer[end_pos:].lstrip()print(f"[STREAM] Found complete JSON object ({len(json_str)} chars): {repr(json_str[:200])}")try:obj = json.loads(json_str)print(f"[STREAM] Successfully parsed JSON object with keys: {list(obj.keys())}")if "response" in obj:response_chunk = obj["response"]objects_yielded += 1response_json = json.dumps(response_chunk)print(f"[STREAM] Yielding object #{objects_yielded} (response size: {len(response_json)} chars)")print(f"[STREAM] Response content preview: {repr(response_json[:200])}")yield f"data: {response_json}\n\n"else:print(f"[STREAM] Object has no 'response' key, skipping")except json.JSONDecodeError as e:print(f"[STREAM] Failed to parse JSON object: {e}")print(f"[STREAM] Problematic JSON: {repr(json_str[:500])}")continueelse:# Incomplete object, wait for more dataprint(f"[STREAM] Incomplete JSON object (brace_count={brace_count}), waiting for more data")breakelse:# Skip unexpected charactersprint(f"[STREAM] Skipping unexpected character: {repr(buffer[0])}")buffer = buffer[1:]print(f"[STREAM] Finished processing. Total chunks: {chunk_count}, total bytes: {total_bytes}, objects yielded: {objects_yielded}")except requests.exceptions.RequestException as e:print(f"Error during streaming request: {e}")error_message = json.dumps({"error": {"message": f"Upstream request failed: {e}"}})5 unmodified linesreturn StreamingResponse(stream_generator(), media_type="text/event-stream")else:resp = requests.post(target_url, data=final_post_data, headers=headers)if resp.status_code == 200:try:google_api_response = resp.json()# The actual response is nested under the "response" key# The actual response is nested under the "response" keystandard_gemini_response = google_api_response.get("response")# The standard client expects a list containing the response objectreturn Response(content=json.dumps([standard_gemini_response]), status_code=200, media_type="application/json")10 unmodified linescreds = get_credentials()if creds:get_user_project_id(creds)print("\nStarting Gemini proxy server on http://localhost:8888")print("Send your Gemini API requests to this address.")uvicorn.run(app, host="0.0.0.0", port=8888)else:print("\nCould not obtain credentials. Please authenticate and restart the server.")2 unmodified lines3456789101112131415161718192021222324256 unmodified lines32333435363738394041424344454647484950515253545556575859606162636465666768697071727374757645 unmodified lines1221231241251261271281291301311321331341351361371381391401411426 unmodified lines149150151152153154155156144 unmodified lines30130230330430530630730830931031131231331431531631731831932032132232332432532614 unmodified lines3413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723 unmodified lines37637737837938038138219 unmodified lines4024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314325 unmodified lines43843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048123 unmodified lines5055065075085095105115125135145155165175185195205215225235245255265275285 unmodified lines53453553653753853954054154254354454554654754854955055155255355455555655755855956056156210 unmodified lines5735745755765775785795805815822 unmodified linesimport requestsimport reimport uvicornimport base64from datetime import datetimefrom fastapi import FastAPI, Request, Response, HTTPException, Dependsfrom fastapi.responses import StreamingResponsefrom fastapi.security import HTTPBasic, HTTPBasicCredentialsfrom http.server import BaseHTTPRequestHandler, HTTPServerfrom urllib.parse import urlparse, parse_qsimport ijsonfrom dotenv import load_dotenvfrom google.oauth2.credentials import Credentialsfrom google_auth_oauthlib.flow import Flowfrom google.auth.transport.requests import Request as GoogleAuthRequest# Load environment variables from .env fileload_dotenv()# --- Configuration ---CLIENT_ID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com"CLIENT_SECRET = "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"6 unmodified linesGEMINI_DIR = os.path.dirname(os.path.abspath(__file__)) # Same directory as the scriptCREDENTIAL_FILE = os.path.join(GEMINI_DIR, "oauth_creds.json")CODE_ASSIST_ENDPOINT = "https://cloudcode-pa.googleapis.com"GEMINI_PORT = int(os.getenv("GEMINI_PORT", "8888")) # Default to 8888 if not setGEMINI_AUTH_PASSWORD = os.getenv("GEMINI_AUTH_PASSWORD", "123456") # Default password# --- Global State ---credentials = Noneuser_project_id = Noneapp = FastAPI()security = HTTPBasic()def authenticate_user(request: Request):"""Authenticate the user with multiple methods."""# Check for API key in query parameters first (for Gemini client compatibility)api_key = request.query_params.get("key")if api_key and api_key == GEMINI_AUTH_PASSWORD:return "api_key_user"# Check for API key in Authorization header (Bearer token format)auth_header = request.headers.get("authorization", "")if auth_header.startswith("Bearer ") and auth_header[7:] == GEMINI_AUTH_PASSWORD:return "bearer_user"# Check for HTTP Basic Authenticationif auth_header.startswith("Basic "):try:encoded_credentials = auth_header[6:]decoded_credentials = base64.b64decode(encoded_credentials).decode('utf-8')username, password = decoded_credentials.split(':', 1)if password == GEMINI_AUTH_PASSWORD:return usernameexcept Exception:pass# If none of the authentication methods workraise HTTPException(status_code=401,detail="Invalid authentication credentials. Use HTTP Basic Auth, Bearer token, or 'key' query parameter.",headers={"WWW-Authenticate": "Basic"},)# Helper class to adapt a generator of bytes into a file-like object# that ijson can read from.45 unmodified linesself.wfile.write(b"<h1>Authentication failed.</h1><p>Please try again.</p>")def get_user_project_id(creds):"""Gets the user's project ID from environment variable, cache, or by probing the API."""global user_project_idif user_project_id:return user_project_id# First, check for environment variable overrideenv_project_id = os.getenv("GEMINI_PROJECT_ID")if env_project_id:user_project_id = env_project_idprint(f"Using project ID from environment variable: {user_project_id}")# Save the environment project ID to cache for consistencysave_credentials(creds, user_project_id)return user_project_id# Second, try to load project ID from credential fileif os.path.exists(CREDENTIAL_FILE):try:with open(CREDENTIAL_FILE, "r") as f:6 unmodified linesexcept Exception as e:print(f"Could not load project ID from cache: {e}")# If not found in environment or cache, probe for itprint("Project ID not found in environment or cache. Probing for user project ID...")headers = {"Authorization": f"Bearer {creds.token}","Content-Type": "application/json",144 unmodified lines@app.post("/{full_path:path}")async def proxy_request(request: Request, full_path: str, username: str = Depends(authenticate_user)):creds = get_credentials()if not creds:return Response(content="Authentication failed. Please restart the proxy to log in.", status_code=500)# Only check if credentials are properly formed and not expired locallyif not creds.valid:if creds.expired and creds.refresh_token:print("Credentials expired locally. Refreshing...")try:creds.refresh(GoogleAuthRequest())save_credentials(creds)print("Credentials refreshed successfully.")except Exception as e:print(f"Could not refresh token during request: {e}")return Response(content="Token refresh failed. Please restart the proxy to re-authenticate.", status_code=500)else:print("Credentials are invalid locally and cannot be refreshed.")return Response(content="Invalid credentials. Please restart the proxy to re-authenticate.", status_code=500)proj_id = get_user_project_id(creds)if not proj_id:return Response(content="Failed to get user project ID.", status_code=500)14 unmodified linesis_streaming = Trueelse:target_url = f"{CODE_ASSIST_ENDPOINT}{path}"# Remove authentication query parameters before forwarding to Google APIquery_params = dict(request.query_params)# Remove our authentication parametersquery_params.pop("key", None)# Add remaining query parameters to target URL if anyif query_params:from urllib.parse import urlencodetarget_url += "?" + urlencode(query_params)try:incoming_json = json.loads(post_data)final_model = model_name_from_url if model_match else incoming_json.get("model")# Set default safety settings to BLOCK_NONE if not specified by usersafety_settings = incoming_json.get("safetySettings")if not safety_settings:safety_settings = [{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},{"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"}]structured_payload = {"model": final_model,"project": proj_id,3 unmodified lines"cachedContent": incoming_json.get("cachedContent"),"tools": incoming_json.get("tools"),"toolConfig": incoming_json.get("toolConfig"),"safetySettings": safety_settings,"generationConfig": incoming_json.get("generationConfig"),},}19 unmodified linesprint(f"[STREAM] Starting streaming request to: {target_url}")print(f"[STREAM] Request payload size: {len(final_post_data)} bytes")# Make the initial streaming requestresp = requests.post(target_url, data=final_post_data, headers=headers, stream=True)print(f"[STREAM] Response status: {resp.status_code}")print(f"[STREAM] Response headers: {dict(resp.headers)}")# If we get a 401, try refreshing the token and retry onceif resp.status_code == 401 and creds.refresh_token:print("[STREAM] Received 401 from Google API. Attempting to refresh token and retry...")resp.close() # Close the failed responsetry:creds.refresh(GoogleAuthRequest())save_credentials(creds)print("[STREAM] Token refreshed successfully. Retrying streaming request...")# Update headers with new token and retryheaders["Authorization"] = f"Bearer {creds.token}"resp = requests.post(target_url, data=final_post_data, headers=headers, stream=True)print(f"[STREAM] Retry request status: {resp.status_code}")except Exception as e:print(f"[STREAM] Could not refresh token after 401 error: {e}")error_message = json.dumps({"error": {"message": "Token refresh failed after 401 error. Please restart the proxy to re-authenticate."}})yield f"data: {error_message}\n\n"returnwith resp:resp.raise_for_status()buffer = ""5 unmodified linesprint(f"[STREAM] Starting to process chunks...")for chunk in resp.iter_content(chunk_size=1024):if isinstance(chunk, bytes):chunk = chunk.decode('utf-8', errors='replace')chunk_count += 1chunk_size = len(chunk) if chunk else 0total_bytes += chunk_sizebuffer += chunk# Process complete JSON objects from the bufferprocessing_iterations = 0while buffer:processing_iterations += 1if processing_iterations > 100: # Prevent infinite loopsbreakbuffer = buffer.lstrip()if not buffer:break# Handle array startif buffer.startswith('[') and not in_array:buffer = buffer[1:].lstrip()in_array = Truecontinue# Handle array endif buffer.startswith(']'):break# Skip commas between objectsif buffer.startswith(','):buffer = buffer[1:].lstrip()continue# Look for complete JSON objectsif buffer.startswith('{'):brace_count = 0in_string = Falseescape_next = False23 unmodified linesjson_str = buffer[:end_pos]buffer = buffer[end_pos:].lstrip()try:obj = json.loads(json_str)if "response" in obj:response_chunk = obj["response"]objects_yielded += 1response_json = json.dumps(response_chunk)yield f"data: {response_json}\n\n"except json.JSONDecodeError as e:continueelse:# Incomplete object, wait for more databreakelse:# Skip unexpected charactersbuffer = buffer[1:]except requests.exceptions.RequestException as e:print(f"Error during streaming request: {e}")error_message = json.dumps({"error": {"message": f"Upstream request failed: {e}"}})5 unmodified linesreturn StreamingResponse(stream_generator(), media_type="text/event-stream")else:# Make the requestresp = requests.post(target_url, data=final_post_data, headers=headers)# If we get a 401, try refreshing the token and retry onceif resp.status_code == 401 and creds.refresh_token:print("Received 401 from Google API. Attempting to refresh token and retry...")try:creds.refresh(GoogleAuthRequest())save_credentials(creds)print("Token refreshed successfully. Retrying request...")# Update headers with new token and retryheaders["Authorization"] = f"Bearer {creds.token}"resp = requests.post(target_url, data=final_post_data, headers=headers)print(f"Retry request status: {resp.status_code}")except Exception as e:print(f"Could not refresh token after 401 error: {e}")return Response(content="Token refresh failed after 401 error. Please restart the proxy to re-authenticate.", status_code=500)if resp.status_code == 200:try:google_api_response = resp.json()# The actual response is nested under the "response" keystandard_gemini_response = google_api_response.get("response")# The standard client expects a list containing the response objectreturn Response(content=json.dumps([standard_gemini_response]), status_code=200, media_type="application/json")10 unmodified linescreds = get_credentials()if creds:get_user_project_id(creds)print(f"\nStarting Gemini proxy server on http://localhost:{GEMINI_PORT}")print("Send your Gemini API requests to this address.")print(f"Authentication required - Password: {GEMINI_AUTH_PASSWORD}")print("Use HTTP Basic Authentication with any username and the password above.")uvicorn.run(app, host="0.0.0.0", port=GEMINI_PORT)else:print("\nCould not obtain credentials. Please authenticate and restart the server.")
2 unmodified lines34562 unmodified linesgoogle-authgoogle-auth-oauthlibrequestsijson2 unmodified lines345672 unmodified linesgoogle-authgoogle-auth-oauthlibrequestsijsonpython-dotenv
.gitignore+3
README.md+120-9
gemini_proxy.py+142-40
requirements.txt+2-1