Gemini Flash 2.0 Error 403: Method doesn't allow unregistered callers

I was initially successful at getting a few requests through API but got 429 because I forgot to add a rate limit in my code.

But since then, I couldn’t make a single successful request, they all error with

[ {
  "error" : {
    "code" : 403,
    "message" : "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
    "status" : "PERMISSION_DENIED"
  }
} ]

Although I pass the API key, of course.

Creating a new API key doesn’t help.

1 Like

Hello!

Could you please clarify that this was working before (the same key, passed the same way), and then it started throwing 403 error?

I don’t know to what extent it “worked”. I had a very naive loop calling to API. When I run it the program exited when it caught 429 error from API. I assume it was on the fifth call (when the 4 calls/min throttle limit was hit), and the first four actually succeeded. Then I fixed the call to do throttling and retires, but didn’t get anything except 403 from the API ever since.

In general, the 403 error may indicate some form of permission issue, for instance your API key is not set properly, doesn’t have the required permissions or you are using the wrong API key. Please verify that your API key is set and has the right access. If the issue persists, please share your code snippet.

Ok I found the problem, this is because in the new version of my code, I use GitHub - hashicorp/go-retryablehttp: Retryable HTTP client in Go client passed into option.WithHTTPClient(), whose doc says that it “takes precedence over other options”.

Turns out API key is passed on the transport level (see dial.newTransport()).

The working version of my code looks like this:

import (
	// ...omitted
	"github.com/tmc/langchaingo/llms/googleai"
	"golang.org/x/time/rate"
	"google.golang.org/api/option"
	http2 "google.golang.org/api/transport/http"
	"net/http"
	// ...omitted
)

	// ...omitted code

		limiter := rate.NewLimiter(rate.Every(GeminiFlashAllowedRatePeriod/GeminiFlashAllowedRate),
			GeminiFlashAllowedRate)
		apiKey := os.Getenv("GEMINI_API_KEY")
		transport, err := http2.NewTransport(context.Background(),
			http.DefaultTransport, option.WithAPIKey(apiKey))
		if err != nil {
			return nil, fmt.Errorf("error creating google api transport: %v", err)
		}
		// this function is defined in other parts of my code, it wraps
		// github.com/hashicorp/go-retryablehttp
		client = NewRetryableClient(&http.Client{
			Transport: NewRateLimitedTransport(transport, limiter),
		})
		// googleai is a very thin wrapper of google's genai package
		llm, err = googleai.New(context.Background(),
			googleai.WithHTTPClient(client),
			googleai.WithAPIKey(apiKey),
			googleai.WithDefaultModel(model))

These http client and transport settings are such a maze and the antithesis to what “simple” golang api design should mean…

1 Like

Cf. genai: custom http client produces an error about "gRPC dial options" · Issue #151 · google/generative-ai-go · GitHub