Integrations
How-to

Connect Google Workspace

Connect Gmail, Calendar, Drive, and Sheets to an Agent through a scoped Google OAuth credential.

For
Google Workspace administrators, Agent operators, and Organization administrators
On this page
  1. What you will configure
  2. Before you begin
  3. Supported services
  4. Choose an access level
  5. Understand the scopes
  6. 1. Choose a Google Cloud project
  7. 2. Enable the required APIs
  8. 3. Configure the consent screen
  9. 4. Create an OAuth client
  10. 5. Connect in Agent Barn
  11. How the flow is protected
  12. What Agent Barn stores
  13. 6. Validate the connection
  14. 7. Verify each service
  15. Runtime behavior
  16. gog is separate from aai-cli
  17. Change the enabled services
  18. Switch Google accounts
  19. Remove Google Workspace
  20. API reference
  21. Troubleshooting
  22. Recommended practices
  23. Next steps
  • Integrations
  • 15–20 minutes
  • Requires a Google OAuth client

Connect Google Workspace to let an Agent work with Gmail, Google Calendar, Google Drive, and Google Sheets.

Agent Barn uses one google_workspace OAuth credential for all selected services. You choose the services, and whether the connection is read-only, before granting access.

What you will configure

  1. Google Cloud project
  2. OAuth client
  3. Consent
  4. Encrypted Agent Secret
  5. gog runtime
Complete connection path
Google Cloud project

        ├── Enable selected APIs
        ├── Configure OAuth consent
        └── Create Web application client


            Agent Barn OAuth flow

                    ├── Select services
                    ├── Select access level
                    └── Grant consent


                    Encrypted Agent Secret


                    Agent start or restart


                         gog CLI

Google Workspace uses the gog CLI. It is separate from aai-cli, and does not use an aai-cli profile.

Before you begin

You need:

  • A Google account that can create or use a Google Cloud project
  • Permission to configure the project’s OAuth consent screen
  • Permission to create OAuth credentials
  • Access to the Google Workspace account the Agent will use
  • Access to configure the Agent
  • agent.update and agent.secret.manage permissions
  • Lifecycle permission when connecting a running Agent
  • Popups allowed for the Agent Barn site

Decide which Google services the Agent needs before creating the connection.

Supported services

One credential can cover any combination of Gmail, Calendar, Drive, and Sheets. The selected services are stored with the credential, and reconstructed in the gog runtime when the Agent starts.

Choose an access level

Agent Barn supports one access level across all services selected in a connection: Read-only or Full access.

Read-only requests view-only scopes. Google enforces the restriction even if the Agent attempts a write operation.

Service Read-only capability Full-access capability
GmailRead and search messagesRead, modify, and manage supported settings
CalendarView calendars and eventsCreate, modify, and delete calendar data
DriveList and read accessible filesCreate, update, and manage accessible files
SheetsRead spreadsheet valuesRead and modify spreadsheets

Understand the requested scopes

Agent Barn derives scopes from the selected services and access level. Identity scopes are always requested, so the connected account can be identified:

Identity scopes
openid
email
https://www.googleapis.com/auth/userinfo.email
Service Read-only scopes Full-access scopes
Gmailgmail.readonlygmail.modify, gmail.settings.basic, gmail.settings.sharing
Calendarcalendar.readonlycalendar
Drivedrive.readonlydrive
Sheetsdrive.readonly, spreadsheets.readonlydrive, spreadsheets

Sheets includes Drive access because gog uses Drive for spreadsheet discovery and export operations. When several services are selected, Agent Barn combines and deduplicates their scopes.

Create or choose a Google Cloud project

  1. Open the Google Cloud Console.
  2. Create a project, or select an existing project.
  3. Confirm that the project belongs to the intended organization.
  4. Record the project name for operational documentation.
  5. Keep the project selected during the remaining setup.

Use a project controlled by your organization, rather than an individual’s temporary project.

Enable the required APIs

Enable the API for every service the Agent will use.

Selected service API to enable
GmailGmail API
CalendarGoogle Calendar API
DriveGoogle Drive API
SheetsGoogle Sheets API and Google Drive API

If you might add another service later, you may enable its API now, but Agent Barn will not request its scopes until it is selected during connection.

Enabling an API does not grant the Agent access. OAuth consent and the connected Google account’s resource permissions still apply.

  1. Open the Google Cloud project’s OAuth consent configuration.
  2. Choose the audience appropriate for your organization.
  3. Enter the application name and support details.
  4. Configure the authorized domain information required by Google.
  5. Set the publishing status to Production, or use Internal for an eligible Google Workspace organization.
  6. Save the consent configuration.

For an External application that remains in Testing, only configured test users can grant access.

Create a Web application OAuth client

  1. Open the project’s API credentials page.
  2. Select Create credentials.
  3. Choose OAuth client ID.
  4. Select Web application.
  5. Enter a recognizable name, such as Agent Barn production.
  6. Add the Agent Barn callback as an authorized redirect URI.
  7. Create the client.
  8. Download the client JSON.

Do not select Desktop app. Agent Barn rejects Desktop client JSON, because its redirect model does not match the website OAuth flow.

Configure the redirect URI

The authorized redirect URI must be an exact match. Use:

Authorized redirect URI
<AGENT_BARN_WEB_ORIGIN>/api/v1/integrations/google/callback

Examples:

Examples
https://agentbarn.example.com/api/v1/integrations/google/callback

http://localhost:4321/api/v1/integrations/google/callback

Use the exact URI displayed in the Google Workspace form for your environment. The following differences cause a mismatch:

  • http instead of https
  • A different hostname
  • A different port
  • A missing /api/v1 segment
  • An extra trailing slash
  • Configuring the API server’s private origin instead of the public web origin

Review the downloaded client JSON

The downloaded file should contain a web object:

Client JSON
{
  "web": {
    "client_id": "REDACTED.apps.googleusercontent.com",
    "client_secret": "REDACTED",
    "redirect_uris": [
      "https://agentbarn.example.com/api/v1/integrations/google/callback"
    ]
  }
}

A Desktop client normally contains an installed object instead. Do not use that file.

Connect Google Workspace in Agent Barn

  1. Open the Agent.
  2. Go to Configuration.
  3. Select Keys & integrations.
  4. Select Edit.
  5. Add Google Workspace under Integration credentials.
  6. Select one or more services.
  7. Choose Read-only or Full access.
  8. Upload the downloaded OAuth client JSON.
  9. Confirm that the client ID and secret were read successfully.
  10. Select Authenticate with Google.
  11. Allow popups if the browser blocks the authorization window.
  12. Choose the Google account the Agent should use.
  13. Review the requested scopes.
  14. Grant all scopes required for the selected services.
  15. Return to Agent Barn.
  16. Confirm that the connected account email is shown.
  17. Apply the Agent configuration.
  18. Restart the Agent if it was running.

Select the services and access level before authentication. They determine what Agent Barn asks Google to approve.

Upload or enter the OAuth client

The connection form supports uploading the client JSON, or entering the client ID and client secret manually. Uploading the downloaded JSON reduces transcription errors.

If entering values manually, provide both the client ID and the client secret. Agent Barn does not accept a partial pair.

The OAuth window requests identity scopes, scopes for each selected service, and offline access. Offline access is required so the Agent can obtain new access tokens after the initial browser session ends.

Google may allow individual scopes to be deselected. Do not remove scopes required by the services selected in Agent Barn.

Agent Barn records the scopes Google actually granted, and rejects a credential whose grant does not cover the selected services at the chosen access level.

How the OAuth flow is protected

  1. Signed state token Records the google_workspace provider and expires after ten minutes
  2. Google redirects the popup Returns an authorization code and the signed state
  3. Callback validates the state The short-lived code is handed to the same-origin opener
  4. Authenticated token exchange The web application exchanges the code for tokens
  5. Encrypted Agent Secret Saved through the normal credential flow

The callback does not expose the client secret or refresh token.

What Agent Barn stores

The Google Workspace credential contains the connected account email, the selected services, the scopes Google granted, the refresh token, the read-only setting, the OAuth client ID, and the OAuth client secret.

The complete payload is encrypted as an Agent Secret. Read APIs return only credential metadata — they do not return the refresh token or client secret.

Validate the connection

After applying the credential:

  1. Open Keys & integrations.
  2. Find Google Workspace.
  3. Select Validate.
  4. Confirm that the expected Google account email is returned.
  5. Review any missing scopes.
  6. Resolve warnings before production use.

Validation exchanges the stored refresh token for an access token, checks the currently available scopes, retrieves the authenticated account identity, compares it with the stored account email, and returns a sanitized result.

Response
{
  "validation_status": "valid",
  "validation_identity": "[email protected]",
  "validation_error": null,
  "missing_scopes": []
}

Verify each service

Gmail

Ask the Agent:

Agent request
Find unread messages received in the last seven days and summarize their senders and subjects.

The underlying gog example is:

Shell
gog gmail search 'is:unread newer_than:7d'

Calendar

Agent request
List today’s calendar events with their start times.
Shell
gog calendar events list --today

For Full access, create a clearly labeled temporary event only after read access has been verified.

Drive

Agent request
List the recent files this Google account can access.
Shell
gog drive files list

The Google account’s Drive permissions still determine which files are visible.

Sheets

Agent request
Read Sheet1 cells A1 through D20 from the test spreadsheet.
Shell
gog sheets values get <spreadsheet-id> 'Sheet1!A1:D20'

Do not grant production spreadsheet access until the test workflow succeeds with a non-sensitive file.

Runtime behavior

Google Workspace is materialized when the Agent starts. Agent Barn:

  1. Decrypts the stored Google Workspace credential.
  2. Creates the gog client configuration.
  3. Imports the refresh token.
  4. Generates a new keyring password.
  5. Adds Google Workspace guidance to the Agent’s AGENTS.md context.
  6. Starts the Agent with the selected services available.
Runtime materialization
Encrypted Agent Secret


Agent start


Fresh ephemeral GOG_HOME


Authenticated gog CLI

The runtime state is stored in an ephemeral GOG_HOME, separate from the Agent’s persistent workspace. Agent Barn wipes and rebuilds GOG_HOME on every start, and the encrypted Agent Secret remains the source of truth.

Removing the credential removes runtime access on the next restart.

gog is separate from aai-cli

Google Workspace uses gog, not aai-cli. The Agent should not:

  • Pass an aai-cli --profile
  • Ask the user to authenticate again
  • Request a token in conversation
  • Use browser automation as a fallback
  • Invent message, file, spreadsheet, or event IDs

The runtime supplies the account and credential automatically. The Agent can discover commands using:

Shell
gog <service> --help

Change the enabled services

To add or remove services:

  1. Open the Google Workspace integration.
  2. Select the desired service set.
  3. Choose the access level.
  4. Authenticate with Google again.
  5. Grant the requested scopes.
  6. Apply the replacement credential.
  7. Restart the Agent.
  8. Validate the connection.
  9. Test each selected service.

Editing the stored service list without reconnecting is not sufficient. The refresh token must cover the new scope set.

Change from read-only to Full access

  1. Confirm that the Agent’s workflow requires write access.
  2. Review the impact with the Google Workspace administrator.
  3. Change the access level to Full access.
  4. Reconnect with Google.
  5. Review the expanded scopes carefully.
  6. Apply and restart.
  7. Validate.
  8. Test a reversible write against non-production data.

To reduce access, reconnect with Read-only, and consider revoking the previous grant from the Google account’s connected-app settings.

Switch Google accounts

  1. Begin a new Google Workspace authentication.
  2. Select the intended Google account in the OAuth window.
  3. Confirm the connected email in Agent Barn.
  4. Apply the replacement credential.
  5. Restart the Agent.
  6. Validate the identity.
  7. Revoke the old account’s grant if it is no longer required.

Agent Barn validation reports an error if the refreshed credential authenticates as a different account than the stored email.

Remove Google Workspace

  1. Stop the Agent, or begin a restart-aware configuration edit.
  2. Open Keys & integrations.
  3. Select Remove for Google Workspace.
  4. Confirm that no assigned Skill requires google_workspace.
  5. Apply the configuration.
  6. Restart the Agent.
  7. Confirm that Google Workspace is no longer available.
  8. Revoke the application grant in the Google account when appropriate.

API reference

Request an authorization URL

This endpoint requires an authenticated user.

HTTP
GET /api/v1/integrations/google/authorize-url

Example query:

HTTP
GET /api/v1/integrations/google/authorize-url?provider=google_workspace&services=gmail,drive&read_only=true&client_id=REDACTED.apps.googleusercontent.com
Response
{
  "authorize_url": "https://accounts.google.com/o/oauth2/v2/auth?..."
}

Supported service identifiers are:

Service identifiers
gmail
calendar
drive
sheets

At least one service is required.

OAuth callback

HTTP
GET /api/v1/integrations/google/callback

Google redirects the popup to this endpoint. Do not call it manually — it validates the signed state and returns the authorization result to the originating browser window.

Exchange the authorization code

This endpoint requires an authenticated user.

HTTP
POST /api/v1/integrations/google/token
Content-Type: application/json
Request body
{
  "code": "REDACTED",
  "client_id": "REDACTED.apps.googleusercontent.com",
  "client_secret": "REDACTED"
}
Response
{
  "refresh_token": "REDACTED",
  "granted_scopes": [
    "openid",
    "email",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/gmail.readonly"
  ],
  "email": "[email protected]"
}

Save the Agent credential

The Agent must be stopped.

HTTP
PATCH /api/v1/organizations/{organization_id}/agents/{agent_id}
Content-Type: application/json
Request body
{
  "secrets": [
    {
      "provider": "google_workspace",
      "content": {
        "email": "[email protected]",
        "services": [
          "gmail",
          "drive"
        ],
        "scopes": [
          "openid",
          "email",
          "https://www.googleapis.com/auth/userinfo.email",
          "https://www.googleapis.com/auth/gmail.readonly",
          "https://www.googleapis.com/auth/drive.readonly"
        ],
        "refresh_token": "REDACTED",
        "read_only": true,
        "client_id": "REDACTED.apps.googleusercontent.com",
        "client_secret": "REDACTED"
      }
    }
  ]
}

The saved scopes must cover every selected service at the configured access level.

Validate the credential

HTTP
POST /api/v1/organizations/{organization_id}/agents/{agent_id}/integrations/google_workspace/validate

Validation requires agent.secret.manage.

Remove the credential

The Agent must be stopped.

Request body
{
  "removed_secret_providers": [
    "google_workspace"
  ]
}

Troubleshooting

The client JSON is rejected as a Desktop client

Use a Web application client

Create an OAuth client of type Web application. Download its JSON and upload the new file.

Desktop clients use an installed JSON object and are not compatible with the Agent Barn callback flow.

Google reports redirect_uri_mismatch

The URI must match exactly

Compare the URI registered in Google Cloud with the exact URI displayed in Agent Barn.

Check the scheme, hostname, port, path, and trailing slash.

Allow popups for the site

Allow popups for the Agent Barn web origin and try again.

The popup must open immediately from the authentication button, so the browser recognizes it as a user-initiated action.

The authorization state is invalid or expired

State expires after ten minutes

The signed state expires after ten minutes. Close the old popup and begin the connection again.

Google authorization was denied

Approve the required scopes

Start again and approve the required scopes.

If the project is in Testing, confirm that the selected account is configured as a test user.

Google did not return a refresh token

An existing grant may be reused

Google may omit a refresh token when the application already has a grant that was created without the required offline behavior.

Remove the application’s access from your Google account permissions, then reconnect through Agent Barn.

Validation reports invalid_grant

The refresh token is no longer usable

The refresh token is invalid, expired, or revoked. Reconnect through Authenticate with Google.

If the OAuth application is in Testing, move it to Production or Internal, because Testing refresh tokens can expire after seven days.

Validation reports missing scopes

Fewer scopes were granted

Google granted fewer scopes than the credential recorded.

Reconnect and approve every scope required by the selected services. Do not deselect individual permissions on the Google consent screen.

Agent Barn says the scopes do not cover the selected services

Selection and grant disagree

The stored service selection and granted scopes do not match. Restart the OAuth flow after selecting the correct services and access level.

Google did not report an account email

Identity scopes are required

Reconnect and approve the identity scopes. Agent Barn requires the account email, because gog stores tokens by account.

Validation reports a different account

The token belongs to another account

The refresh token currently authenticates as another Google account. Reconnect using the intended account and replace the credential.

Google OAuth is not configured on this server

Client values or web URL missing

Confirm that the form supplied both the OAuth client ID and client secret.

For a deployment-managed client, the self-hosted Agent Barn configuration must provide its Google OAuth client values and the correct public web application URL.

Authentication succeeds, but a command says the API is disabled

Enable the API in the same project

Enable the corresponding API in the same Google Cloud project that owns the OAuth client.

Sheets workflows may also require the Google Drive API.

The Agent does not know that Google Workspace is connected

Configuration is generated at start

Apply the credential and restart the Agent. Google Workspace runtime configuration and policy context are generated at Agent start.

A write command is refused

The credential may be read-only

Check whether the credential is read-only. Read-only scopes intentionally prevent changes.

Reconnect with Full access only when the workflow requires it.

The Agent cannot access a particular file or calendar

Scopes are not resource permissions

OAuth scopes permit a category of operations, but the connected Google account must also have access to the specific resource.

Share the resource with the connected account, or choose a different account.

The connection stops working after seven days

Consent screen is still in Testing

The OAuth consent screen is probably still in Testing status. Move it to Production or Internal, revoke the old grant, and reconnect.

  • Use a Google account dedicated to the Agent’s role
  • Create the OAuth client in an organization-controlled project
  • Use an Internal consent screen when appropriate
  • Move External applications out of Testing for durable connections
  • Select only the required services
  • Prefer read-only access
  • Enable only the necessary Google APIs
  • Copy the redirect URI exactly
  • Protect the downloaded client JSON
  • Verify the connected account email
  • Validate after connecting or rotating credentials
  • Test with non-sensitive mail, calendars, files, and spreadsheets
  • Restart the Agent after every credential change
  • Revoke superseded grants through the Google account
  • Never paste refresh tokens or client secrets into Agent conversations
  • Review Google Cloud audit and OAuth usage regularly

Next steps

Continue to Connect GitHub to configure repository access with a scoped personal access token.

Documentation