使用 Google 的 Generative AI 服务时,请求没有包含足够的认证范围(scopes)

题意:

Google generativeai 403 Request had insufficient authentication scopes. [reason: "ACCESS_TOKEN_SCOPE_INSUFFICIENT"

问题背景:

I have tried the simple POC for generativeai on its own to do 'generate_content' and it works. Now I am trying to integrate the generativeai to existing project and just trying to get the model only and it is throwing this.

import google.generativeai as genai

genai.configure(api_key=GOOGLE_GEMINI_API)
model_name = "gemini-1.5-flash"
model = genai.GenerativeModel(model_name=model_name)
response = model.generate_content('Hello there.')


# Error
google.api_core.exceptions.PermissionDenied: 403 Request had insufficient authentication scopes. [reason: "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
domain: "googleapis.com"
metadata {
  key: "service"
  value: "generativelanguage.googleapis.com"
}
metadata {
  key: "method"
  value: "google.ai.generativelanguage.v1beta.GenerativeService.GenerateContent"
}

I am suspecting that there is a conflict of ADC (Application Default Credential) issue. Cause the existing project is configured with ADC. However I want to leave it alone and I want google gemini API key to be a separate module and I don't want it tied to existing project. (Nor I want yet another google cloud project to manage at this point.) Still, getting google gemini key, it has created google cloud project nonetheless. Are there a way to make generativeai API not picking up the credential from the existing ADC ? I am using the Python library. I need help from google cloud platform oauth credential expert on this infamous area, I think.

Here is the google generativeai code on github

generative-ai-python/google/generativeai/client.py at v0.7.0 · google-gemini/generative-ai-python · GitHub

问题解决:

Every time i have seen "ACCESS_TOKEN_SCOPE_INSUFFICIENT" error it has been due to the fact that i wasn't not sending an api key or that the env far that I was sending was null.

genai.configure(api_key=GOOGLE_GEMINI_API)

Personally i normal use a .env file for my env vars and use pip python-dotenv

I believe though that if you are loading from a linux environment you can use

my_var = os.environ.get('MY_ENV_VAR')

As I have personally had this issue before. I have submitted a feature request to the client library #450

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 05:26:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 05:26:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 05:26:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 05:26:03       69 阅读

热门阅读

  1. 【算法】反转链表

    2024-07-12 05:26:03       27 阅读
  2. NoSQL之REDIS配置与优化

    2024-07-12 05:26:03       20 阅读
  3. 阿里云API安全2.0全新发布

    2024-07-12 05:26:03       24 阅读
  4. (三)大模型/人工智能/机器学习/深度学习/NLP

    2024-07-12 05:26:03       19 阅读
  5. 数据库常见问题(持续更新)

    2024-07-12 05:26:03       25 阅读
  6. DevSecOps在数字政府建设中的实践研究

    2024-07-12 05:26:03       25 阅读
  7. Flask 不同版本项目的终端命令运行方式

    2024-07-12 05:26:03       22 阅读
  8. 使用GPT-4和ChatGPT构建应用项目

    2024-07-12 05:26:03       22 阅读
  9. 65、Flink 的 DataStream Connectors 概述

    2024-07-12 05:26:03       24 阅读
  10. stm32使用pwm和编码器模式(包含重映射)

    2024-07-12 05:26:03       25 阅读