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
- Google Cloud project
- OAuth client
- Consent
- Encrypted Agent Secret
- gog runtime
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 CLIGoogle 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.updateandagent.secret.managepermissions- 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 |
|---|---|---|
| Gmail | Read and search messages | Read, modify, and manage supported settings |
| Calendar | View calendars and events | Create, modify, and delete calendar data |
| Drive | List and read accessible files | Create, update, and manage accessible files |
| Sheets | Read spreadsheet values | Read 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:
openid
email
https://www.googleapis.com/auth/userinfo.email| Service | Read-only scopes | Full-access scopes |
|---|---|---|
| Gmail | gmail.readonly | gmail.modify, gmail.settings.basic, gmail.settings.sharing |
| Calendar | calendar.readonly | calendar |
| Drive | drive.readonly | drive |
| Sheets | drive.readonly, spreadsheets.readonly | drive, 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
- Open the Google Cloud Console.
- Create a project, or select an existing project.
- Confirm that the project belongs to the intended organization.
- Record the project name for operational documentation.
- 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 |
|---|---|
| Gmail | Gmail API |
| Calendar | Google Calendar API |
| Drive | Google Drive API |
| Sheets | Google 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.
Configure the OAuth consent screen
- Open the Google Cloud project’s OAuth consent configuration.
- Choose the audience appropriate for your organization.
- Enter the application name and support details.
- Configure the authorized domain information required by Google.
- Set the publishing status to Production, or use Internal for an eligible Google Workspace organization.
- 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
- Open the project’s API credentials page.
- Select Create credentials.
- Choose OAuth client ID.
- Select Web application.
- Enter a recognizable name, such as
Agent Barn production. - Add the Agent Barn callback as an authorized redirect URI.
- Create the client.
- 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:
<AGENT_BARN_WEB_ORIGIN>/api/v1/integrations/google/callbackExamples:
https://agentbarn.example.com/api/v1/integrations/google/callback
http://localhost:4321/api/v1/integrations/google/callbackUse the exact URI displayed in the Google Workspace form for your environment. The following differences cause a mismatch:
httpinstead ofhttps- A different hostname
- A different port
- A missing
/api/v1segment - 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:
{
"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
- Open the Agent.
- Go to Configuration.
- Select Keys & integrations.
- Select Edit.
- Add Google Workspace under Integration credentials.
- Select one or more services.
- Choose Read-only or Full access.
- Upload the downloaded OAuth client JSON.
- Confirm that the client ID and secret were read successfully.
- Select Authenticate with Google.
- Allow popups if the browser blocks the authorization window.
- Choose the Google account the Agent should use.
- Review the requested scopes.
- Grant all scopes required for the selected services.
- Return to Agent Barn.
- Confirm that the connected account email is shown.
- Apply the Agent configuration.
- 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.
Complete Google consent
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
- Signed state token Records the google_workspace provider and expires after ten minutes
- Google redirects the popup Returns an authorization code and the signed state
- Callback validates the state The short-lived code is handed to the same-origin opener
- Authenticated token exchange The web application exchanges the code for tokens
- 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:
- Open Keys & integrations.
- Find Google Workspace.
- Select Validate.
- Confirm that the expected Google account email is returned.
- Review any missing scopes.
- 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.
{
"validation_status": "valid",
"validation_identity": "[email protected]",
"validation_error": null,
"missing_scopes": []
}Verify each service
Gmail
Ask the Agent:
Find unread messages received in the last seven days and summarize their senders and subjects.The underlying gog example is:
gog gmail search 'is:unread newer_than:7d'Calendar
List today’s calendar events with their start times.gog calendar events list --todayFor Full access, create a clearly labeled temporary event only after read access has been verified.
Drive
List the recent files this Google account can access.gog drive files listThe Google account’s Drive permissions still determine which files are visible.
Sheets
Read Sheet1 cells A1 through D20 from the test spreadsheet.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:
- Decrypts the stored Google Workspace credential.
- Creates the
gogclient configuration. - Imports the refresh token.
- Generates a new keyring password.
- Adds Google Workspace guidance to the Agent’s
AGENTS.mdcontext. - Starts the Agent with the selected services available.
Encrypted Agent Secret
│
▼
Agent start
│
▼
Fresh ephemeral GOG_HOME
│
▼
Authenticated gog CLIThe 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:
gog <service> --helpChange the enabled services
To add or remove services:
- Open the Google Workspace integration.
- Select the desired service set.
- Choose the access level.
- Authenticate with Google again.
- Grant the requested scopes.
- Apply the replacement credential.
- Restart the Agent.
- Validate the connection.
- 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
- Confirm that the Agent’s workflow requires write access.
- Review the impact with the Google Workspace administrator.
- Change the access level to Full access.
- Reconnect with Google.
- Review the expanded scopes carefully.
- Apply and restart.
- Validate.
- 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
- Begin a new Google Workspace authentication.
- Select the intended Google account in the OAuth window.
- Confirm the connected email in Agent Barn.
- Apply the replacement credential.
- Restart the Agent.
- Validate the identity.
- 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
- Stop the Agent, or begin a restart-aware configuration edit.
- Open Keys & integrations.
- Select Remove for Google Workspace.
- Confirm that no assigned Skill requires
google_workspace. - Apply the configuration.
- Restart the Agent.
- Confirm that Google Workspace is no longer available.
- Revoke the application grant in the Google account when appropriate.
API reference
Request an authorization URL
This endpoint requires an authenticated user.
GET /api/v1/integrations/google/authorize-urlExample query:
GET /api/v1/integrations/google/authorize-url?provider=google_workspace&services=gmail,drive&read_only=true&client_id=REDACTED.apps.googleusercontent.com{
"authorize_url": "https://accounts.google.com/o/oauth2/v2/auth?..."
}Supported service identifiers are:
gmail
calendar
drive
sheetsAt least one service is required.
OAuth callback
GET /api/v1/integrations/google/callbackGoogle 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.
POST /api/v1/integrations/google/token
Content-Type: application/json{
"code": "REDACTED",
"client_id": "REDACTED.apps.googleusercontent.com",
"client_secret": "REDACTED"
}{
"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.
PATCH /api/v1/organizations/{organization_id}/agents/{agent_id}
Content-Type: application/json{
"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
POST /api/v1/organizations/{organization_id}/agents/{agent_id}/integrations/google_workspace/validateValidation requires agent.secret.manage.
Remove the credential
The Agent must be stopped.
{
"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.
The OAuth popup does not open
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.
Recommended practices
- 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.