-DefaultCredentialsError - generativeai

I am trying to run the PALM API Python Example:

<

import pprint
import google.generativeai as palm


API_KEY ='XXXXXXXXXXXXXXXXXXXXXXX'
palm.configure(api_key = API_KEY)

 

 

model = 'models/text-bison-001'
prompt = """
You are an expert at solving word problems.

Solve the following problem:

I have three houses, each with three cats.
each cat owns 4 mittens, and a hat. Each mitten was
knit from 7m of yarn, each hat from 4m.
How much yarn was needed to make all the items?

Think about it step by step, and show your work.
"""

completion = palm.generate_text(
model=model,
prompt=prompt,
temperature=0,
# The maximum length of the response
max_output_tokens=800,
)

print(completion.result)

i>

I keep getting the following error message:

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS

I have doubled checked the APi-Key  and have enabled it.  The code is similar to 2 examples found with googe search.

Solved Solved
2 5 4,058
1 ACCEPTED SOLUTION

I finally got it working on a different computer. I suspected that there was something corrupt in one of the modules. Created a req.txt file containing the following modules:
cachetools-5.3.1 google-ai-generativelanguage-0.2.0 google-api-core-2.11.1 google-auth-2.22.0 google-generativeai-0.1.0 googleapis-common-protos-1.59.1 grpcio-1.56.2 grpcio-status-1.56.2 proto-plus-1.22.3 protobuf-4.23.4 pyasn1-0.5.0 pyasn1-modules-0.3.0 rsa-4.9
Ran:
pip uninstall -y -r reqs.txt
pip install google-generativeai

It is now working as expected

View solution in original post

5 REPLIES 5

I got around the google.auth.exceptions.DefaultCredentialsError: by creating an ADC json file with the following command:

gcloud auth application-default login --project palm-demo-393701 --scopes https://www.googleapis.com/auth/cloud-platform

I now get the following error:

google.api_core.exceptions.PermissionDenied: 403 Request had insufficient authentication scopes. [reason: "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
domain: "googleapis.com"
metadata {
key: "method"
value: "google.ai.generativelanguage.v1beta2.TextService.GenerateText"
}
metadata {
key: "service"
value: "generativelanguage.googleapis.com"

 

I have the exact same problem as Hankp (saw you on stack overflow). I've looked through this entire site of scopes (https://developers.google.com/identity/protocols/oauth2/scopes) trying to find one relating to google generative client, and I've found nothing

I finally got it working on a different computer. I suspected that there was something corrupt in one of the modules. Created a req.txt file containing the following modules:
cachetools-5.3.1 google-ai-generativelanguage-0.2.0 google-api-core-2.11.1 google-auth-2.22.0 google-generativeai-0.1.0 googleapis-common-protos-1.59.1 grpcio-1.56.2 grpcio-status-1.56.2 proto-plus-1.22.3 protobuf-4.23.4 pyasn1-0.5.0 pyasn1-modules-0.3.0 rsa-4.9
Ran:
pip uninstall -y -r reqs.txt
pip install google-generativeai

It is now working as expected

I am getting the same error for QuickStart example.

Please provide a solution who doesn't have a different computer. 

 

Little late to the party but theres some scopes documented here in the API doc - https://ai.google.dev/api/rest/v1beta/models/generateContent