Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

GoogleGenerativeAIError: ACCESS_TOKEN_SCOPE_INSUFFICIENT

Hi Community,

I'm encountering an error while attempting to make a POST request to the Google Generative Language API. I’m working on a project that requires embedding text using the embedding-001 model. Below are the details:
Screenshot of the all the roles provided and enabled for service account:

sidsanc_0-1729616677311.png

  • Issue: I'm trying to embed text using the Google Generative AI API, but I keep receiving the following error:

 

 

{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
        "domain": "googleapis.com",
        "metadata": {
          "method": "google.ai.generativelanguage.v1beta.GenerativeService.BatchEmbedContents",
          "service": "generativelanguage.googleapis.com"
        }
      }
    ]
  }
}

 

 

What I've tried so far:

  • I’ve generated a service account key and added the necessary scopes for Generative Language API:

 

 

scopes = ["https://www.googleapis.com/auth/generative-language"]
​

 

 

  • I’ve used the following authentication method:

 

 

credentials = service_account.Credentials.from_service_account_file(
    service_account_key_path, scopes=scopes
)
credentials.refresh(Request())
​

 

 

  • The access token seems to be generated correctly, but the API call fails with insufficient scope permissions.

    Error Response: Despite following the official documentation, I continue to receive the ACCESS_TOKEN_SCOPE_INSUFFICIENT error. I’ve verified the service account and scopes multiple times.

    Questions:

    • Am I missing any specific permissions or scopes for the Generative AI API?
    • Is there any additional configuration required for embedding text using the embedding-001 model?

 



1 1 1,030
1 REPLY 1

Hi @sidsanc,

Welcome to Google Cloud Community!

The error message you are getting signifies that you are encountering permission issues. This typically means that your project does not have the necessary authorization to access or modify the requested resource. 

Please ensure that your account has the appropriate access permissions for the required scopes and double-check the authentication method you're using for your specific environment.

The posts below address a similar error message and may be helpful:

For further information about the different authentication methods and use cases, please check the GCP Authentication documentation.

I hope this helps.