[Python] Issue with using Gemini AI API

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python script which creates a local chatbot server on my PC and is trying to use Google Gemini AI via my API.

    Here are the details:

    # Trying to access Gogle Gemini AI via my API:
    base_url = "https://generativelanguage.googleapis.com/v1beta/models/"
    model = "gemini-1.5-flash"
    task = "generateContent"
    endpoint = ".apps.googleusercontent.com"
    google_client_id = "{GOOGLE_CLIENT_ID}" # Stored in a variable declared and set at startup
    # Constructing the URL
    url = f"{base_url}{model}:{task}?key={GOOGLE_CLIENT_ID}{endpoint}"


    The above url is correctly constructed, but I still get an error message:


    Gemini API Error: 401 Client Error: # Unauthorized for {see the above url}
    ​

    I am running my script in PowerShell on my local PC. All imports have been made.

    from fastapi import FastAPI, WebSocket
    from fastapi.websockets import WebSocketDisconnect
    import asyncio
    import json
    import requests
    import webbrowser
    import uvicorn
    import threading
    import websockets
    import os
    import google.generativeai as genai


    ... I have also tried with Vertex AI, but that is even more complicated and didn't give any results.

    Continue reading...

Compartilhe esta Página