Skip to main content

Google Cloud OAuth app setup

This guide is for DevOps / platform operators who configure Fiko's backend before agents can click "Connect with Google" in the dashboard. Follow every numbered step in order.

Before you start

You need:

Google account with access to Google Cloud Console

Use a company account, not a personal Gmail that only you control.

Fiko messaging-service public URL

Production: https://msg.fikosrv.com

Access to edit .env on fiko-messaging-service and fiko-core-service

Both services need the same OAuth client ID/secret and encryption key.

Gmail ingest: polling vs Pub/Sub

Fiko supports two ways to detect new Gmail mail. Both work — Pub/Sub is faster for production.

ModeEnv requiredLatencyWhen to use
Polling (default)OAuth only — no GMAIL_PUBSUB_TOPIC~1 minuteFallback if Pub/Sub not configured yet
Pub/Sub push (real-time)GMAIL_PUBSUB_TOPIC + push subscription per channelSecondsProduction — recommended

Step-by-step setup

Setup time: 20–30 minutes OAuth only; +15 minutes for Pub/Sub (production)

Part A — Create project and enable APIs

  1. 1Open console.cloud.google.com and sign in.
  2. 2Top bar → click the project dropdownNew Project.
    • Project name: Fiko Email (or your product name)
    • Click Create and wait until the project is ready
    • Select the new project from the top bar dropdown
  3. 3Left menu → APIs & ServicesLibrary.
  4. 4Search Gmail API → open it → click Enable.
  5. 5Back to Library → search Cloud Pub/Sub APIEnable.

    Expected result

    Both Gmail API and Cloud Pub/Sub API should show status Enabled. Pub/Sub is required for real-time push in Part G — enable it now so you do not need to return later.

Part B — OAuth consent screen

  1. 6Left menu → APIs & ServicesOAuth consent screen.
  2. 7Choose User type:
    • External — for any Gmail / Google Workspace customer (most cases)
    • Internal — only if every mailbox is in your single Workspace org
  3. 8Fill App information → click Save and Continue:
    FieldExample valueNotes
    App nameFikoShown on Google consent screen
    User support email[email protected]Must be a Google account you control
    App logo(optional)Not required for Testing
    Application home pagehttps://fiko.comYour product URL
    Application privacy policy linkhttps://fiko.com/privacyRequired before Production publish
    Application terms of service linkhttps://fiko.com/termsRequired before Production publish
    Authorized domainsfiko.com, fikosrv.comDomains used in links above
    Developer contact information[email protected]Google may email about verification
  4. 9On Scopes → click Add or Remove Scopes → filter "Gmail" → add both scopes below (full URLs):

    OAuth scopes Fiko requests

    Scope URL (exact)Purpose in Fiko
    https://www.googleapis.com/auth/gmail.modifyRead inbox, mark read/unread, move messages — ingest + threading
    https://www.googleapis.com/auth/gmail.sendSend replies from Fiko on behalf of the connected mailbox
    Fiko authorize URL also sets access_type=offline and prompt=consent so refresh tokens are issued reliably. Then UpdateSave and Continue.
  5. 10On Test users (while app is in Testing) → Add users → enter every support mailbox email that will connect during pilot (e.g. [email protected]).

    Expected result

    In Testing mode, only listed test users can complete OAuth. Refresh tokens may expire after ~7 days until you publish to Production — see CASA ops guide.
  6. 11Click Back to Dashboard. Publishing status should show Testing.
Figure 1 — OAuth consent screen: add gmail.modify and gmail.send under Scopes, keep Testing until CASA is complete.

Part C — Create OAuth client (Web)

  1. 12Left menu → APIs & ServicesCredentials.
  2. 13Click Create CredentialsOAuth client ID.
  3. 14Application type: Web application. Name: Fiko Gmail.
  4. 15Under Authorized redirect URIsAdd URI → paste exactly:
    https://msg.fikosrv.com/v2/email/oauth/google/callback

    Expected result

    The URI must match GOOGLE_OAUTH_CALLBACK_URL character-for-character (including http vs https, no trailing slash).
  5. 16Click Create. A popup shows Client ID and Client secret. Copy both immediately — you cannot view the secret again later.
Figure 2 — OAuth client ID (Web): Authorized redirect URI must exactly match GOOGLE_OAUTH_CALLBACK_URL.

Part D — Generate encryption key and set env vars

  1. 17Generate EMAIL_ENCRYPTION_KEY (32 random bytes, base64). Run once and reuse the same value on both services:
    node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"

    Expected result

    Store in secrets manager. If this key changes, existing stored OAuth refresh tokens cannot be decrypted — agents must reconnect mailboxes.
  2. 18On fiko-messaging-service, set:
    GOOGLE_OAUTH_CLIENT_ID=123456789-xxxx.apps.googleusercontent.com
    GOOGLE_OAUTH_CLIENT_SECRET=GOCSPX-xxxxxxxx
    GOOGLE_OAUTH_CALLBACK_URL=https://msg.fikosrv.com/v2/email/oauth/google/callback
    EMAIL_ENCRYPTION_KEY=your-base64-32-byte-key
    GMAIL_PUBSUB_TOPIC=projects/YOUR_GCP_PROJECT/topics/gmail-push
    • GMAIL_PUBSUB_TOPIC: set after Part G topic is created, or leave unset for polling-only mode
    • GOOGLE_OAUTH_CALLBACK_URL must match Google Console redirect URI exactly
  3. 19On fiko-core-service, copy client ID, secret, and encryption key (callback URL not required on core):
    GOOGLE_OAUTH_CLIENT_ID=...
    GOOGLE_OAUTH_CLIENT_SECRET=...
    EMAIL_ENCRYPTION_KEY=...
  4. 20Redeploy / restart both services after updating env.

Part E — Verify OAuth

  1. 21In Fiko dashboard → AppsEmailAdd newConnect mailboxConnect Google.
  2. 22After Allow, browser redirects back to Fiko. Finish setup → connected mailbox shown.

    Expected result

    If you see redirect_uri_mismatch, fix URI in Google Console vs GOOGLE_OAUTH_CALLBACK_URL.
  3. 23Send test email → open Chats → reply from Fiko.

Part G — Gmail Pub/Sub push (production, recommended)

Production flow: Gmail users.watch (labelIds: INBOX) → Google Pub/Sub topic → POST /v2/email/gmail/push/:channelId → immediate sync. Cron at 2 AM renews watch (Gmail watch expires in ≤7 days).

  1. 24Pub/Sub → TopicsCreate topic → ID: gmail-push.
  2. 25Topic → PermissionsGrant access → principal [email protected] → role Pub/Sub Publisher → Save.

    Expected result

    Without this permission, Gmail users.watch fails silently or logs permission errors.
  3. 26Set env on messaging-service and redeploy:
    GMAIL_PUBSUB_TOPIC=projects/YOUR_GCP_PROJECT/topics/gmail-push
  4. 27Connect (or reconnect) Gmail channel in Fiko — after OAuth attach, Fiko calls Gmail users.watch with the topic above.

    Expected result

    messaging-service log: Gmail Pub/Sub watch enabled for channel ...
  5. 28Get channel ID from Fiko (Apps → Email → Channel Details → Provider channel ID or URL).
  6. 29Pub/Sub → SubscriptionsCreate subscription — fill every field:

    Push subscription fields

    FieldValueNotes
    Subscription IDgmail-push-{channelId}One subscription per Gmail channel
    Select a Cloud Pub/Sub topicgmail-pushTopic from step 24
    Delivery typePushNot Pull
    Endpoint URLhttps://msg.fikosrv.com/v2/email/gmail/push/{channelId}Replace {channelId} with real UUID
    Enable authenticationUncheckedFiko endpoint is public HTTPS; OIDC optional later
    Enable payload unwrappingCheckedDelivers Gmail notification JSON directly
    Acknowledgement deadline10 seconds (default)Increase only if Fiko sync is slow
    Message retention duration7 days (default)
    Retry policyRetry immediately (default)
    Dead-letter topic(none)Optional for ops monitoring

    Expected result

    Each new Gmail channel needs a new push subscription because the endpoint path includes :channelId.
  7. 30Send test email → chat should appear within seconds.

Fiko endpoints (Google)

EndpointMethodPurpose
https://msg.fikosrv.com/v2/email/oauth/google/callbackGET / POSTOAuth redirect after user clicks Allow
https://msg.fikosrv.com/v2/email/gmail/push/{channelId}POSTPub/Sub push when new mail arrives

Environment variables reference

VariableServiceExample
GOOGLE_OAUTH_CLIENT_IDmessaging + corexxx.apps.googleusercontent.com
GOOGLE_OAUTH_CLIENT_SECRETmessaging + coreGOCSPX-...
GOOGLE_OAUTH_CALLBACK_URLmessaging only.../v2/email/oauth/google/callback
EMAIL_ENCRYPTION_KEYmessaging + coreBase64 32-byte key (same value both services)
GMAIL_PUBSUB_TOPICmessaging onlyprojects/.../topics/gmail-push — bật Pub/Sub push (Part G)

Troubleshooting

Error 400: redirect_uri_mismatch

Google Console redirect URI ≠ GOOGLE_OAUTH_CALLBACK_URL. Open Credentials → your OAuth client → fix URI → save → retry OAuth.

Access blocked: app has not completed verification

User is not on Test users list, or app needs Production publish. Add email under OAuth consent → Test users.

Mail arrives slowly (minutes instead of seconds)

Check Part G — push subscription endpoint must match channelId and GMAIL_PUBSUB_TOPIC must be set before connect/reconnect.

Pub/Sub push not reaching Fiko

Verify push subscription endpoint URL, HTTPS is publicly reachable, and [email protected] has Pub/Sub Publisher on the topic.

Test configuration fails in Fiko

Core-service missing GOOGLE_OAUTH_* or EMAIL_ENCRYPTION_KEY. Both must match messaging-service.

Never commit Client Secret or EMAIL_ENCRYPTION_KEY to git. Use secrets manager in production.

Google Cloud OAuth App Setup — Email Integration