pythongoogle-colaboratorygoogle-gemini

API Key of Google Gemini is not working on Google collab


I was create an api key, and I was following this tutorial. When I replace 'GOOGLE_API_KEY' in cell code bellow:

# Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
GOOGLE_API_KEY=userdata.get('[REDACTED]')

genai.configure(api_key=GOOGLE_API_KEY)

I got an error:

---------------------------------------------------------------------------
SecretNotFoundError                       Traceback (most recent call last)
<ipython-input-17-e4a84a54faa3> in <cell line: 2>()
      1 # Or use `os.getenv('GOOGLE_API_KEY')` to fetch an environment variable.
----> 2 GOOGLE_API_KEY=userdata.get('[REDACTED]')
      3 
      4 genai.configure(api_key=GOOGLE_API_KEY)

/usr/local/lib/python3.10/dist-packages/google/colab/userdata.py in get(key)
     51     )
     52   if not resp.get('exists', False):
---> 53     raise SecretNotFoundError(key)
     54   if not resp.get('access', False):
     55     raise NotebookAccessError(key)

SecretNotFoundError: Secret '[REDACTED]' does not exist.

I tried creating API keys in my account but it not working.


Solution

  • You need to click the key on the left bar in Colab

    enter image description here

    then when you run this it should output your key

    from google.colab import userdata
    # The gemini api key
    API_KEY = userdata.get('API_KEY')
    print(API_KEY)