# Authenticate to Google Workspace

Create a GoogleWorkspaceSession in .NET from a service account, a short-lived access token, or existing Google APIs credentials.

**Packages**

- [OfficeIMO.GoogleWorkspace](https://www.nuget.org/packages/OfficeIMO.GoogleWorkspace) `dotnet add package OfficeIMO.GoogleWorkspace`
- [OfficeIMO.GoogleWorkspace.Auth.GoogleApis](https://www.nuget.org/packages/OfficeIMO.GoogleWorkspace.Auth.GoogleApis) `dotnet add package OfficeIMO.GoogleWorkspace.Auth.GoogleApis`

**Works with**

- .NET
- [PowerShell](/docs/pswriteoffice/install/) `Install-Module PSWriteOffice`

**Target frameworks**

netstandard2.0, net8.0, net10.0, net472

**APIs**

- [`GoogleWorkspaceSession`](/api/google-workspace/officeimo-googleworkspace-googleworkspacesession/)
- [`GoogleApisCredentialSource`](/api/google-workspace-auth/officeimo-googleworkspace-auth-googleapis-googleapiscredentialsource/)
- [`GoogleInstalledApplicationAuthorization`](/api/google-workspace-auth/officeimo-googleworkspace-auth-googleapis-googleinstalledapplicationauthorization/)
- [`GoogleWorkspaceAccessToken`](/api/google-workspace/officeimo-googleworkspace-googleworkspaceaccesstoken/)
- [`GoogleWorkspaceCredentialBindingResolver`](/api/google-workspace/officeimo-googleworkspace-googleworkspacecredentialbindingresolver/)
- [`IGoogleWorkspaceCredentialSource`](/api/google-workspace/officeimo-googleworkspace-igoogleworkspacecredentialsource/)
- [`IGoogleWorkspaceTokenStore`](/api/google-workspace-auth/officeimo-googleworkspace-auth-googleapis-igoogleworkspacetokenstore/)
- [`GoogleWorkspaceSessionOptions`](/api/google-workspace/officeimo-googleworkspace-googleworkspacesessionoptions/)
- [`GoogleServiceAccountCredentialSource`](/api/google-workspace/officeimo-googleworkspace-googleserviceaccountcredentialsource/)
- [`StaticAccessTokenCredentialSource`](/api/google-workspace/officeimo-googleworkspace-staticaccesstokencredentialsource/)

**Cmdlets**

- [`New-OfficeGoogleWorkspaceSession`](/api/powershell/new-officegoogleworkspacesession/)

**Limits**

OfficeIMO provides no plaintext refresh-token store; the application must encrypt authorization state at rest.

**Verified**

Examples compiled against the source on 2026-11-14

**Data**

- [OfficeIMO.GoogleWorkspace API index](/api/google-workspace/index.json)
- [OfficeIMO.GoogleWorkspace.Auth.GoogleApis API index](/api/google-workspace-auth/index.json)
- [PSWriteOffice command catalog](/data/powershell/commands.json)

Every translator works through a `GoogleWorkspaceSession`, which gets tokens from an `IGoogleWorkspaceCredentialSource`. `OfficeIMO.GoogleWorkspace` includes a service-account source with optional domain-wide delegation, a static token source, and a delegate source. `OfficeIMO.GoogleWorkspace.Auth.GoogleApis` adapts `Google.Apis.Auth` credentials for applications that already use them.

## Use a service account

The service-account source is the usual choice for services that create or change files. With domain-wide delegation, `SubjectUser` is the user the service account acts as.

### C#

```csharp
using OfficeIMO.GoogleWorkspace;

var options = new GoogleWorkspaceSessionOptions {
    ApplicationName = "Reporting service",
    SubjectUser = "analyst@example.com",
    UseDomainWideDelegation = true
};

using var credentials = GoogleServiceAccountCredentialSource.FromFile("service-account.json", options);
var session = new GoogleWorkspaceSession(credentials, options);
```

### PowerShell

```powershell
$session = New-OfficeGoogleWorkspaceSession -KeyPath .\Secrets\reporting.json -Subject analyst@example.com -Scope Documents, DriveFile
```

## Use a short-lived access token

A static token is enough for read-only samples and short jobs. An account name or scope supplied by the caller is not proof of what the token can do, so a static token cannot authorize changes. For changes from a delegate or the Google APIs adapter, return `GoogleWorkspaceAccessToken.FromVerifiedCredential` after checking the provider-issued token evidence.

### C#

```csharp
using OfficeIMO.GoogleWorkspace;

string token = Environment.GetEnvironmentVariable("GOOGLE_WORKSPACE_ACCESS_TOKEN")
    ?? throw new InvalidOperationException("Set GOOGLE_WORKSPACE_ACCESS_TOKEN.");

var session = new GoogleWorkspaceSession(
    new StaticAccessTokenCredentialSource(token, DateTimeOffset.UtcNow.AddMinutes(50)),
    new GoogleWorkspaceSessionOptions { ApplicationName = "Read-only report" });
```

## Use Google APIs credentials

Install `OfficeIMO.GoogleWorkspace.Auth.GoogleApis` when the application already uses `Google.Apis.Auth`. `GoogleApisCredentialSource` adapts `GoogleCredential`, `UserCredential`, or `ITokenAccess`. For sessions that change files, supply a `GoogleWorkspaceCredentialBindingResolver` that verifies the token with Google and returns its account and grants. `GoogleInstalledApplicationAuthorization.AuthorizeAsync` runs the installed-application flow with PKCE and needs that resolver plus an `IGoogleWorkspaceTokenStore` your application provides.

`GoogleWorkspaceSession` checks the verified evidence against `ExpectedAccount` and the exact scopes of each operation, so a policy written for one account cannot authorize a token issued to another.

Keep access tokens, service-account keys, OAuth client secrets, and refresh tokens out of source control, and request the smallest scopes the workflow needs.

<!-- PowerForge markdown artifact sha256:F87F3D46B90867FBB5E1812EDF961C0A78A2052A311EADF6511AB92A30BEFDB6 -->
