Google Gemini API error: "DefaultCredentialsError: Your default credentials were not found."

953 Views Asked by At

I want to use Google APIs without default credentials. Is there any other way to work with APIs directly?

The error:

DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.
Traceback:
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
    exec(code, module.__dict__)
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/App.py", line 63, in <module>
    response=get_gemini_response(input_prompt,image_data,input)
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/App.py", line 16, in get_gemini_response
    response=model.generate_content([input,image[0],prompt])
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/generativeai/generative_models.py", line 241, in generate_content
    self._client = client.get_default_generative_client()
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/generativeai/client.py", line 230, in get_default_generative_client
    return _client_manager.get_default_client("generative")
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/generativeai/client.py", line 161, in get_default_client
    client = self.make_client(name)
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/generativeai/client.py", line 121, in make_client
    client = cls(**self.client_config)
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/client.py", line 433, in __init__
    self._transport = Transport(
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/transports/grpc.py", line 150, in __init__
    super().__init__(
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/ai/generativelanguage_v1beta/services/generative_service/transports/base.py", line 98, in __init__
    credentials, _ = google.auth.default(
File "/Users/pulkitrajput/PycharmProjects/InVoice-Extractor/venv/lib/python3.9/site-packages/google/auth/_default.py", line 691, in default
    raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
4

There are 4 best solutions below

2
On

There are 2 ways you can use the Gemini API:

  1. Google Generative AI SDK (no need to set the credentials)
  2. Google Vertex AI SDK (you need to set the credentials)

Note: I'll show you both ways in case someone else with the same error finds this question and is interested in solving the error instead of choosing the approach that doesn't require setting the credentials.

Google Generative AI SDK

I've made a YouTube tutorial on how to use the Gemini Pro API and posted the code on my GitHub profile.

Python example:

import google.generativeai as genai
import os

genai.configure(api_key=os.environ['GOOGLE_CLOUD_API_KEY'])

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content('Say hi')

print(response.text)

Node.js example:

const { GoogleGenerativeAI } = require("@google/generative-ai");

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_CLOUD_API_KEY);

async function run() {
  const model = genAI.getGenerativeModel({ model: "gemini-pro" });
  const result = await model.generateContent("Say hi");
  const response = await result.response;

  console.log(response.text());
}

run();

Google Vertex AI SDK

Problem

As the error says, you haven't set the Application Default Credentials (i.e., ADC).

There are 3 ways you can set the ADC:

  1. GOOGLE_APPLICATION_CREDENTIALS environment variable
  2. User credentials set up by using the Google Cloud CLI
  3. The attached service account, returned by the metadata server

Solution

I'll explain how to set the ADC using the GOOGLE_APPLICATION_CREDENTIALS environment variable.

STEP 1: Click CREATE SERVICE ACCOUNT to create a service account for your Google Cloud project

Step 1

STEP 2: Click on the service once it is created and go to the KEYS tab

Step 2

STEP 3: Click ADD KEY and create a new key

Step 3

STEP 4: Select JSON and click CREATE

Step 4

STEP 5: Go to your Downloads folder and you should see the key (i.e., JSON file) there

STEP 6: Set the key (i.e., JSON file) as an environment variable, as the key should be downloaded from your browser automatically

Variable name: GOOGLE_APPLICATION_CREDENTIALS
Variable value: C:\Users\PATH\TO\YOUR\KEY\xxxxxxxxxxxxxxxxx.json

Don't forget to restart the computer after you set the key as an environment variable!


Now, ADC will search for the GOOGLE_APPLICATION_CREDENTIALS environment variable and look into the JSON file to see if we are permitted to access resources or not.

Also, I've made a YouTube tutorial on how to use the Vertex AI Gemini Pro API, where I also covered this step of setting the ADC, and posted the code on my GitHub profile.

2
On

Using this helped me resolve this issue

import os
os.environ["GOOGLE_API_KEY"] = "your_api_key"
0
On

I had this problem following the documentation and I was unable to use the SDK without creating an application on Google Cloud. However, it is possible to use the genAi REST API with the google key.

Google genAI REST API

0
On

I finally found the solution after so many frustrating hours. I was getting this error whenever i tried to use gemini-pro with api, where it asked me to install gcloud and setup Application Default Credentials (ADC) in the environment or through a json file.

And whenever i tried doing that i got thrown in an error 403: Permission denied, token ** error. And the documentation suggested that i should be able to chose one or the other to use the api or ADC.

The fix simply was import os did not recognize my exported key and i had to export the key into the venv environment itself.