google / generative-ai-go

Go SDK for Google Generative AI
Apache License 2.0
596 stars 60 forks source link

ACCESS_TOKEN_SCOPE_INSUFFICIENT error #36

Closed samarth-na closed 10 months ago

samarth-na commented 10 months ago

I've copied the code directly from the quicstart-guide for golang

package main

import (
    "context"
    "fmt"
    "github.com/google/generative-ai-go/genai"
    "google.golang.org/api/option"
    "log"
    "os"
)

func main() {
    ctx := context.Background()
    // Access your API key as an environment variable (see "Set up your API key" above)
    client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("AIzaSyB1Nx3jxDqWOflvh---JnyEndKx3kWYwefbvw")))
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    // For text-only input, use the gemini-pro model
    model := client.GenerativeModel("gemini-pro")
    resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a magic backpack."))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(resp)
}

this is identical with the one one shown i guide throws this error

error details: name = ErrorInfo 
reason = ACCESS_TOKEN_SCOPE_INSUFFICIENT 
domain = googleapis.com 
metadata = map[method:google.ai.generativelanguage.v1.GenerativeService.GenerateContent service:generativelanguage.googleapis.com]
exit status 1

but this runs fine if I try this in python

import google.generativeai as genai

genai.configure(api_key="AIzaSyA25dW4h_UcxwgWkOXeq2zpOqeudqdces3XMU")

for m in genai.list_models():
  if 'generateContent' in m.supported_generation_methods:
    print(m.name)

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("What is the meaning of life?")

print(response.text)

i don't know why is this happening if there was some prereqvisits is should have been mentioned in the quick-start guide if anyone can give an solution please help

eliben commented 10 months ago

client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("AIzaSyB1Nx3jxDqWOflvh---JnyEndKx3kWYwefbvw")))

This line tries to read an env var with the name of your API key, which is probably not what you intended.

If you want to hardcode the key into your code, just pass it to WithAPIKey (without os.Getenv)

Or alternatively use the quickstart code exactly, but set an API_KEY env var with your key as value.