Giter Club home page Giter Club logo

Comments (10)

zongsforce avatar zongsforce commented on May 31, 2024 1

Okay I fixed it... somehow. Basically, I just switched to a different computer, did the exact same thing, and it worked first try. I suppose that means it was something wrong with the python version or the pip install? I'm not really sure, but I'm not going to stress too hard about it. Thank you for your help.

The situation I encountered is similar to yours. I encountered the same error when using virtualenv, but when I switched to pyenv with the same code, it worked fine. In addition, you don't even need to install Google Cloud CLI since you have the API key. Thank you for your inspiration.

from generative-ai-python.

markmcd avatar markmcd commented on May 31, 2024

Can you just stick with the API key and ignore the ADCs? API key is the easiest approach if you can use it.

If you must use ADCs, you can use gcloud to add the scopes you need (but it'd be helpful for us if you could reply with some info on why API key isn't sufficient):

gcloud auth application-default login --scopes="https://www.googleapis.com/auth/generative-language,https://www.googleapis.com/auth/cloud-platform"

But don't mix the two - either use API key to auth (and remove the environmental references to the ADCs) or use the ADCs (and don't set api_key=...)

There's some more detail in this comment.

from generative-ai-python.

valenmoore avatar valenmoore commented on May 31, 2024

Okay I tried not setting ADCs, just using an API key. I followed the documentation letter for letter. My API key works perfectly when I run the following curl script:

curl -H 'Content-Type: application/json'
-d '{"prompt": {text: "Give me five subcategories of jazz"} }'
"https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key=${XXXXXXXXXXXXXX}"

but when I run this code (straight from the documentation):

import google.generativeai as palm
import os
os.environ['API_KEY'] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
palm.configure(api_key=os.environ['API_KEY'])

response = palm.generate_text(prompt="The opposite of hot is")
print(response.result) # 'cold.'

I still get that same error message about token scopes. What should I try? Thank you!

from generative-ai-python.

valenmoore avatar valenmoore commented on May 31, 2024

Also, I tried adding the scopes that you linked in the ADC gcloud code to the project with oauth, but it said they were not valid. I don't know if I am doing something wrong there or what but yeah.

from generative-ai-python.

markmcd avatar markmcd commented on May 31, 2024

The client libraries try to use the ADCs up if they are present in the environment - they don't have to be explicitly set. You can remove them using gcloud (log out), delete the files, or just pop the environment var:

import os
# Hide ADCs from Google's API client library
os.environ.pop('GOOGLE_APPLICATION_CREDENTIALS', None)

Maybe there's something we can do better here to avoid the ADCs if an API key is set - I'll leave this open as a FR to "don't use ADCs if an API key is specified".

from generative-ai-python.

valenmoore avatar valenmoore commented on May 31, 2024

Used this code and it still did not work, so I started fresh with a new project. Just used API key, no ADC stuff. Same story, the API key worked with the curl function but I still got the same error when I ran the code. Seems like I've tried everything here so I'm not sure where to go next. Thanks anyways.

from generative-ai-python.

valenmoore avatar valenmoore commented on May 31, 2024

Okay I fixed it... somehow. Basically, I just switched to a different computer, did the exact same thing, and it worked first try. I suppose that means it was something wrong with the python version or the pip install? I'm not really sure, but I'm not going to stress too hard about it. Thank you for your help.

from generative-ai-python.

MarkDaoust avatar MarkDaoust commented on May 31, 2024

I think some of the misunderstanding is because GOOGLE_APPLICATION_CREDENTIALS aren't always necessary.

When I've used ADC with the client libraries I've never set GOOGLE_APPLICATION_CREDENTIALS, but I did have to set it when trying to use the Node.js client libraries.

gcloud auth application-default login is probably putting the file in a standard location, and the client library is picking it up from there. So I think it's normal that the env-var makes no difference.

But trying it out:

  1. It's easy to trigger the 403 Request had insufficient authentication scopes. error.
  2. But if I pass an API-key, that takes precedence, and it works fine.

So I think this is working fine.

From @hankp46's #51 he said he fixed the same error by pinning the versions of a bunch of the required libraries.

I think that's a clue.

API-KEY support is somewhat new, and and it's possible older versions of one of the helpers libs is causing the problem. Since pip is conservative about upgrading packages, you probably had an old version installed, and pip just kept that? That would explain @zongsforce's observations as well since pyenv installs a whole new python, it would have picked up new versions of the packages?

The culprit package is not google-auth, 1.24 fails hard on some other error (too old) and 1.25 passes.

@vayvaychicken @zongsforce : Can either of you post the output of pip freeze from an environment where it's failing like this?

from generative-ai-python.

JosephRivera517 avatar JosephRivera517 commented on May 31, 2024

When I run the app in my local using the API Key, it works perfectly well. But when I run it using github, I got this error: 403 Request had insufficient authentication scopes.

from generative-ai-python.

MarkDaoust avatar MarkDaoust commented on May 31, 2024

Hi, I didn't change anything to fix this, but API-keys are rellatively new in google APIs.

I remember when it used to fail like this, but it isn't for me now. Maybe this was fixed lower in the stack.

Do you still have this problem?

from generative-ai-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.