Skip to content

Gemini API Key is not being passed Correctly #186

@TheLinuxMaster

Description

@TheLinuxMaster

Describe the bug

I'm trying to send a simple request, but every time i execute the code, the key is always empty. also there is virtually no documentation on gemini provider

 public async Task<string> Chat(ChatRequest request)
        {
            string messsage = "";
            using (var client = new HttpClient())
            {
                GoogleConfiguration config = new()
                {
                    ApiKey = ""
                };
                GoogleProvider provider = new(config, client);

                GoogleChatModel model = new(provider, "gemini-2.0-flash");

                StringBuilder sb = new StringBuilder();

                await foreach (var response in model.GenerateAsync("What is the meaning of life"))
                {
                    sb.Append(response);
                }

                messsage = sb.ToString();
            }

            return messsage;
        }

but the response i get is this:

GenerativeAI.Exceptions.GenerativeAIException: Error while requesting https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=__API_Key__:

{
  "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"
  }
}

   at GenerativeAI.Models.ModelBase.GenerateContent(String apiKey, String model, GenerateContentRequest request) in D:\GitHub\Google_GenerativeAI\src\GenerativeAI\Models\ModelBase.cs:line 68
   at GenerativeAI.Models.GenerativeModel.GenerateContentAsync(GenerateContentRequest request, CancellationToken cancellationToken) in D:\GitHub\Google_GenerativeAI\src\GenerativeAI\Models\GenerativeModel.cs:line 157
   at LangChain.Providers.Google.GoogleChatModel.CreateChatCompletionAsync(IReadOnlyCollection`1 messages, CancellationToken cancellationToken) in /_/src/Google/src/GoogleChatModel.cs:line 89
   at LangChain.Providers.Google.GoogleChatModel.GenerateAsync(ChatRequest request, ChatSettings settings, CancellationToken cancellationToken)+MoveNext() in /_/src/Google/src/GoogleChatModel.cs:line 151
   at LangChain.Providers.Google.GoogleChatModel.GenerateAsync(ChatRequest request, ChatSettings settings, CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource<System.Boolean>.GetResult()
   at BotTalk.Services.Implementation.WhatsAppService.Chat(ChatRequest request) in D:\BotTalk\BotTalk\Services\Implementation\WhatsAppService.cs:line 40
   at BotTalk.Services.Implementation.WhatsAppService.Chat(ChatRequest request) in D:\BotTalk\BotTalk\Services\Implementation\WhatsAppService.cs:line 40
   at BotTalk.Controllers.WhatsAppController.Get(ChatRequest request) in D:\BotTalk\BotTalk\Controllers\WhatsAppController.cs:line 25
   at lambda_method5(Closure, Object)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS
=======
Accept: text/plain
Host: localhost:7248
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: en-US,en;q=0.9
Content-Type: application/json
Origin: https://localhost:7248
Referer: https://localhost:7248/swagger/index.html
Content-Length: 60
sec-ch-ua-platform: "Windows"
sec-ch-ua: "Microsoft Edge";v="135", "Not-A.Brand";v="8", "Chromium";v="135"
sec-ch-ua-mobile: ?0
sec-fetch-site: same-origin
sec-fetch-mode: cors
sec-fetch-dest: empty
priority: u=1, i

Steps to reproduce the bug

replace the api key and execute the code.

public async Task<string> Chat(ChatRequest request)
        {
            string messsage = "";
            using (var client = new HttpClient())
            {
                GoogleConfiguration config = new()
                {
                    ApiKey = ""
                };
                GoogleProvider provider = new(config, client);

                GoogleChatModel model = new(provider, "gemini-2.0-flash");

                StringBuilder sb = new StringBuilder();

                await foreach (var response in model.GenerateAsync("What is the meaning of life"))
                {
                    sb.Append(response);
                }

                messsage = sb.ToString();
            }

            return messsage;
        }

Expected behavior

No response

Screenshots

No response

NuGet package version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions