User guide
Create a Google OAuth application and register its credentials before asking users to connect their mailboxes. Use the callback address for the Open Mercato instance they sign in to.
This is the administrator guide for enabling Gmail in Open Mercato. You do the Google Cloud parts once per workspace; after that, each user connects their own mailbox from their profile page (see the Communication Channels end-user guide).
There are two layers:
Info · Who connects what
The Client ID / Secret you create here is tenant-wide – one OAuth app serves every user in the workspace. Individual users never see it; they just click Connect Gmail.
https://app.example.com (or http://localhost:3000 for local dev).Google Cloud Console → search "Gmail API" → Enable. (This is separate from OAuth; if it's off you'll get a SERVICE_DISABLED error after connecting.)
APIs & Services → OAuth consent screen (newer console: Google Auth Platform → Branding / Audience).
User type: External (use Internal only if every user is in the same Google Workspace org).
App information: app name, support email, developer contact.
Scopes → Add or remove scopes – add exactly these three:
| Scope | Why |
|---|---|
https://www.googleapis.com/auth/gmail.modify | Read, send, and label mail |
https://www.googleapis.com/auth/userinfo.email | Identify the connected address |
https://www.googleapis.com/auth/userinfo.profile | Display name |
Test users – while the app is in Testing status, only listed test users can connect. Add every email that will connect a mailbox (yourself included).
Warning · gmail.modify is a restricted scope
Restricted scopes have two consequences:
gmail.modify that includes a CASA security assessment. Budget weeks for the review.APIs & Services → Credentials → Create credentials → OAuth client ID.
Application type: Web application.
Authorized redirect URIs – add your callback URL exactly:
https://app.example.com/api/communication_channels/oauth/gmail/callback
For local dev also add:
http://localhost:3000/api/communication_channels/oauth/gmail/callback
Create, then copy the Client ID and Client secret.
In Open Mercato: Integrations → Gmail → Credentials (or Configure).
| Field | Value |
|---|---|
| OAuth Client ID | The Client ID from Part 1, step 3 |
| OAuth Client Secret | The Client secret |
| OAuth Scopes | Leave blank – uses the gmail.modify defaults. Only set this if you deliberately requested different scopes on the consent screen. |
Save. The Gmail integration should now show Healthy.
Tip · "This provider is not configured yet" on connect?
That error means the Client ID/Secret aren't saved (or were saved under a different organization). Re-open Integrations → Gmail → Credentials, confirm both fields are filled, and save again while scoped to the workspace your users belong to.
Each user: Profile → My communication channels → Connect Gmail → approve the consent screen.
Warning · Approve the Gmail permission
On the consent screen, grant the Gmail access (not just basic profile). If only profile is granted, the channel comes up in Error with "insufficient authentication scopes". The fix is to Disconnect and Connect Gmail again, approving the Gmail permission. (Open Mercato sends prompt=consent, so Google always re-shows the screen.)
While the app is unverified you'll see a "Google hasn't verified this app" screen → Advanced → Go to … (unsafe) → Continue. That's expected in Testing mode.
Once connected the channel shows Connected, and new inbox mail appears within ~60s. That's a complete, working setup – Part 4 is optional.
By default inbound mail arrives by polling (~60s). Push delivery uses Gmail → Cloud Pub/Sub → an Open Mercato webhook to cut that to 5–15s. It's more moving parts; skip it unless you want real-time.
Caution · Push needs a public HTTPS URL
Google's Pub/Sub cannot deliver to localhost. For local testing, expose your dev server with a tunnel like ngrok (ngrok http 3000) and use the public URL below – see Local development.
Throughout, your webhook URL is:
https://app.example.com/api/communication_channels/webhooks/gmail
Google Cloud Console → search "Pub/Sub" (it's a separate product from the OAuth screens) → Topics → Create topic, e.g. gmail-inbound. Its full name is projects/<PROJECT_ID>/topics/gmail-inbound.
On the topic → Permissions → Add principal:
[email protected] (this is Gmail itself)Without this grant, Re-register push fails – gmail.users.watch can't publish to your topic.
IAM & Admin → Service Accounts → Create service account, e.g. gmail-push-invoker. No project roles are needed. Note its email – gmail-push-invoker@<PROJECT_ID>.iam.gserviceaccount.com.
This is the identity Pub/Sub uses to call your webhook; Open Mercato verifies it.
Pub/Sub → Subscriptions → Create subscription (or from the topic):
| Field | Value |
|---|---|
| Topic | projects/<PROJECT_ID>/topics/gmail-inbound |
| Delivery type | Push |
| Endpoint URL | https://app.example.com/api/communication_channels/webhooks/gmail |
| Enable authentication | Yes |
| Service account | gmail-push-invoker@<PROJECT_ID>.iam.gserviceaccount.com (from step 3) |
| Audience | the same webhook URL as Endpoint URL |
If prompted to grant the Pub/Sub service agent the Service Account Token Creator role, accept it – Pub/Sub needs it to mint the OIDC token.
In your app environment (e.g. apps/mercato/.env):
OM_GMAIL_PUBSUB_TOPIC=projects/<PROJECT_ID>/topics/gmail-inbound
OM_GMAIL_PUBSUB_AUDIENCE=https://app.example.com/api/communication_channels/webhooks/gmail
OM_GMAIL_PUBSUB_SERVICE_ACCOUNT_EMAIL=gmail-push-invoker@<PROJECT_ID>.iam.gserviceaccount.com
| Variable | Must equal |
|---|---|
OM_GMAIL_PUBSUB_TOPIC | The topic from step 1 – passed to gmail.users.watch |
OM_GMAIL_PUBSUB_AUDIENCE | The Audience you set on the subscription (step 4) – byte-for-byte identical |
OM_GMAIL_PUBSUB_SERVICE_ACCOUNT_EMAIL | The subscription's service-account email (step 3) |
Restart the app so the variables load.
When OM_GMAIL_PUBSUB_TOPIC is set, new connections auto-register push. For an already-connected mailbox, open Profile → My communication channels and click Re-register push. On success the channel shows Push active.
Info · Watch renewal is automatic
A Gmail watch expires after ~7 days. Open Mercato renews it daily (a background job re-issues gmail.users.watch before expiry, tuned by OM_PUSH_RENEWAL_GMAIL_LEAD_HOURS). You don't renew it manually. If push ever fails, the channel silently falls back to polling so mail still flows.
ngrok http 3000 → copy the https://<random>.ngrok-free.app URL.https://<random>.ngrok-free.app/api/communication_channels/webhooks/gmail as both the subscription Endpoint URL and the Audience and OM_GMAIL_PUBSUB_AUDIENCE.localhost (your redirect URI is localhost:3000/...); ngrok is only for the inbound webhook.The OAuth consent screen is in Testing and your address isn't a test user. OAuth consent screen → Test users → Add users → add the connecting email → retry.
The granted token is missing the Gmail scope. Disconnect the channel, then Connect Gmail again and approve the Gmail permission on the consent screen. Confirm gmail.modify is added under the consent screen's scopes.
The Client ID/Secret aren't resolvable. Re-save them in Integrations → Gmail → Credentials (Part 2).
missing_topic)OM_GMAIL_PUBSUB_TOPIC isn't set. This is harmless – polling delivers mail anyway. Complete Part 4 only if you want real-time push.
wrong_audience (403)The subscription's Audience and OM_GMAIL_PUBSUB_AUDIENCE differ. Make them byte-for-byte identical (both equal to the full webhook URL).
The subscription's service account doesn't match OM_GMAIL_PUBSUB_SERVICE_ACCOUNT_EMAIL. Align them (step 3 ↔ step 5).
Almost always the missing Publisher grant – confirm [email protected] has Pub/Sub Publisher on the topic (Part 4, step 2).