Gmail sync: connection and renewal
This is an advanced reference for administrators. Most setups do not need it. The everyday page is Enable faster Gmail synchronization.
This page describes what happens to a Gmail mailbox after push is enabled: how the watch is registered and renewed, what happens when notifications stop, and how to check the token.
Who this is for: MailDesk and Odoo administrators.
You need: MailDesk Pro and the Mailbox administrator group. Push reuses the Gmail OAuth connection used for normal sync and asks for no new permissions.
The lifecycle at a glance
| Phase | What happens | Who does it |
|---|---|---|
| Connect | The mailbox is connected over Gmail OAuth and syncs. | Administrator, one time |
| Watch registered | users.watch is registered against your Pub/Sub topic. |
Setup assistant |
| Active | Google publishes a notification on each inbox change. The webhook triggers a sync. | Automatic |
| Renew | The watch is refreshed before the 7-day limit. | Background job |
| Silence, then fallback | If notifications stop, a catch-up sync is requested and scheduled sync continues. | Background job |
| Token lost | Registering or renewing fails with a clear reason. Scheduled sync continues. | You reconnect |
Connect: no new permissions
Before it registers a watch, MailDesk checks that:
- the account is a Gmail account;
- it has a connected OAuth server with a stored refresh token;
- the token's scopes include
https://mail.google.com/orgmail.modify.
If a check fails, registration stops with a reason. The scope gmail.modify, which already powers sync, also covers users.watch.
Watch registered: what is stored
MailDesk calls users.watch for the INBOX label against the topic in your project. It then saves one push-subscription record for the account:
- the topic name as the watched resource;
- the
historyIdthat Gmail returns, used as the starting point of the first incremental fetch; - the expiry date;
- state active, with the last error cleared and the failure counter reset.
MailDesk stores no Google Cloud secret. Only the project id, topic name and signer service-account email are stored, together with the webhook settings.
Active: how one message arrives
- Gmail publishes a notification to your Pub/Sub topic.
- Pub/Sub sends it to
<web.base.url>/maildesk/push/gmail. The route accepts POST requests only and is public. The body can be at most 64 KB. -
The webhook checks the request:
Check Failure An Authorization: Bearertoken is presentHTTP 401 The audience and signer allow-list are configured HTTP 503 The OIDC signature, the audience and the signer allow-list are valid HTTP 403 The body is no larger than 64 KB HTTP 413 The token and body are not logged. 4. MailDesk runs its normal incremental Gmail sync for that mailbox and answers
202 Accepted. 5. The open MailDesk screen refreshes over Odoo's websocket.
The configuration parameters behind step 3 are maildesk.pro.push.gmail.audience (falls back to maildesk.pro.push.gmail.webhook_url) and maildesk.pro.push.gmail.allowed_signer_emails. The assistant sets them. Do not edit them by hand unless support asks you to.
Renew
A Gmail watch lasts at most 7 days. You never renew it by hand.
- A job runs about once an hour. It renews every active watch that expires within the next 24 hours, so a watch is renewed at about 6 days.
- Renewal runs
users.watchagain against the stored topic and updates the expiry, the last-renewal time and the state. - If renewal fails, MailDesk records the error, increases a failure counter and tries again on the next run.
Silence and fallback
Pub/Sub delivers at least once, but a notification can still be lost.
- The same hourly job flags every active subscription that has not received a notification in the last 6 hours. It asks for a catch-up sync of the account.
- The Gmail incremental job runs about every 2 minutes, so mail still arrives on the normal schedule.
Token revoked, expired or disconnected
A refresh token stops working when the user revokes MailDesk in their Google account, the OAuth client secret changes, the token expires, or the scopes no longer cover users.watch.
| Situation | What MailDesk does | What you do |
|---|---|---|
| Token invalid or expired | Registering and renewing fail with an OAuth reason. | Reconnect the mailbox and accept all permissions, then open the assistant again. |
| Scope missing | Registering fails with a scope reason. | Reconnect and accept all requested scopes. |
| Mailbox disconnected | The watch cannot be renewed. | Reconnect the mailbox and run the assistant again. |
Mail is not lost in any of these cases. Scheduled sync keeps the inbox current.
Deleting a message in Gmail (moving it to Bin) is also a mailbox change. The same path removes it from the open inbox without a reload.
Check the token: Verify token now
Odoo shows a green "Token Valid" badge when a refresh token exists, even if the token no longer works. MailDesk replaces it with a neutral "Gmail token stored" badge and adds two buttons on the mail server form:
| Button | What it does |
|---|---|
| Verify token now | Makes a real refresh request to Google. Reports a fresh access token, a missing token, or a failure that needs a reconnect. |
| Reconnect Gmail | Starts the Google sign-in again. Administrators only. It names missing OAuth app fields if they are not configured. |
Use Verify token now first when sync or push looks stuck.
The realtime test timeline
The last assistant step shows six checkpoints, each as observed, missing or waiting:
| # | Checkpoint | Where to look if it stalls |
|---|---|---|
| 1 | Pub/Sub delivered the notification to your webhook | Reachability and Pub/Sub setup |
| 2 | MailDesk verified the OIDC signature and audience | Audience and signer allow-list |
| 3 | Gmail history sync was triggered | Sync jobs |
| 4 | The message was saved in MailDesk's mail store | Sync and ingestion |
| 5 | The message body was prepared in advance | See How email previews are stored |
| 6 | The message is visible in the MailDesk inbox | Sync and ingestion |
Expected steady state
- The mailbox shows realtime Active with an expiry date.
- The watch renews itself about every 6 days.
- If push pauses, scheduled sync keeps mail flowing.
If it does not work
| Symptom | Likely cause | Safe next step |
|---|---|---|
| Push worked, then went quiet | A notification was lost | The silence check requests a catch-up automatically. Wait for the scheduled sync. |
| Realtime shows an OAuth error | The token was revoked or expired | Verify token now. If it fails, Reconnect Gmail, then open the assistant. |
| The watch stopped renewing | The token is invalid, or the topic was deleted | Verify the token, check that the topic exists, then run the assistant again. |
| Timeline stalls at 1 | The webhook is not reachable or Pub/Sub is wrongly set up | Run preflight. The push URL must answer a GET with 405. |
| Timeline stalls at 2 | Audience or signer allow-list mismatch | Run validation again. |
| Deleted mail still shows | The open screen was not refreshed | Reload the view. |
More: Fix missing or delayed emails and Fix Gmail synchronization setup errors.