Google_GenerativeAI.Auth 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Google_GenerativeAI.Auth --version 2.0.2                
NuGet\Install-Package Google_GenerativeAI.Auth -Version 2.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Google_GenerativeAI.Auth" Version="2.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Google_GenerativeAI.Auth --version 2.0.2                
#r "nuget: Google_GenerativeAI.Auth, 2.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Google_GenerativeAI.Auth as a Cake Addin
#addin nuget:?package=Google_GenerativeAI.Auth&version=2.0.2

// Install Google_GenerativeAI.Auth as a Cake Tool
#tool nuget:?package=Google_GenerativeAI.Auth&version=2.0.2                

Google_GenerativeAI.Auth

NuGet License: MIT

Google_GenerativeAI.Auth is a helper library for the Unofficial C# Google Generative AI SDK (Google Gemini) that simplifies authentication using OAuth and Service Accounts. It provides concrete implementations for the IGoogleAuthenticator interface, making it easy to integrate authentication into your applications using the SDK. This library handles the complexities of obtaining access tokens, so you can focus on building generative AI applications.

Features

  • Service Account Authentication: Authenticate using Google Service Accounts, either with a JSON key file or with a key and password.
  • IGoogleAuthenticator Implementation: Seamlessly integrates with the Google Generative AI SDK by implementing the IGoogleAuthenticator interface.
  • Easy-to-Use API: Provides simple methods for creating authenticators and retrieving access tokens.
  • Asynchronous Support: Uses async/await for non-blocking, asynchronous operations.

Installation

Install the Google_GenerativeAI.Auth NuGet package:

dotnet add package Google_GenerativeAI.Auth

You will also need to install the main Google Generative AI SDK:

dotnet add package Google_GenerativeAI  

Usage

The Google_GenerativeAI.Auth library provides two main ways to authenticate with a service account:

  1. Using a JSON Key File (Recommended):
    Uses the path to a service account JSON key file. This is the preferred method for most use cases.

  2. Using Key and Password:
    Requires the service account email, key, and password.

1. Service Account Authentication (JSON Key File)

This is the recommended approach. Download a JSON key file for your service account from the Google Cloud Console. Keep this file secure!

// Assuming 'jsonFilePath' is the path to your service account JSON key file.
var authenticator = new GoogleServiceAccountAuthenticator(jsonFilePath);
var vertexAi = new VertexAIModel(authenticator: authenticator);

Important: jsonFilePath should be the full path to your JSON key file. Do not hardcode this path. Instead, retrieve it from an environment variable or a secure configuration store:

2. Service Account Authentication (Key and Password)

This method is less common and requires careful handling of the credentials.

// Assuming 'email', 'key', and 'password' are your service account credentials.
var authenticator = new GoogleServiceAccountAuthenticator(email, key, password);
var vertexAi = new VertexAIModel(authenticator: authenticator);

Important: email, key, and password should be your actual service account credentials. Do not hardcode these values. Use environment variables or a secure configuration system:

2. OAuth Authentication with Client Secret and Client Id

// Assuming 'credentialFile' your client_secret.json file.
var authenticator = new GoogleOAuthAuthenticator(credentialFile);
var vertexAi = new VertexAIModel(authenticator: authenticator);

Getting the Access Token Directly

You can get the access token directly from the IGoogleAuthenticator if needed:

var token = await authenticator.GetAccessTokenAsync();
Console.WriteLine(token.AccessToken); // Use the token as needed.

Important Security Considerations

  1. NEVER hardcode credentials directly in your source code. Use environment variables, configuration files, or a secrets management service (like Azure Key Vault or AWS Secrets Manager).
  2. Protect your service account JSON key file. Store it securely and do not commit it to version control. Treat it like a password. Add it to your .gitignore file.
  3. Grant the least privilege necessary to your service account. Only give it the permissions required to access the Google Generative AI APIs.

Dependencies

Contributing

Contributions are welcome! Please open an issue to discuss proposed changes or create a pull request.

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Google_GenerativeAI.Auth:

Package Downloads
Google_GenerativeAI.Web

This library is part of the Google_GenerativeAI SDK and provides .NET Web Application integration for seamless usage of the Google_GenerativeAI SDK.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.14 0 2/19/2025
2.0.11 59 2/18/2025
2.0.7 133 2/17/2025
2.0.6 71 2/17/2025
2.0.4 65 2/16/2025
2.0.2 60 2/16/2025
2.0.0 74 2/16/2025