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)
There are 2 ways you can use the Gemini API:
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:
Node.js example:
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:
GOOGLE_APPLICATION_CREDENTIALS
environment variableSolution
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 2: Click on the service once it is created and go to the KEYS tab
STEP 3: Click ADD KEY and create a new key
STEP 4: Select JSON and click CREATE
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
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.