# Connect

Connect a Google account to a profile for any mix of Gmail, Google Calendar, Google Drive and Google Contacts, and widen it later.

## Connect a Google account

Request the authorization link for a profile with the areas you need, then send the user to `authUrl`:

```bash
curl "https://zernio.com/api/v1/connect/google?profileId=PROFILE_ID&scopes=email,calendar&redirect_url=https://example.com/done" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```

```json
{ "authUrl": "https://accounts.google.com/o/oauth2/v2/auth?client_id=…", "state": "…" }
```

The user picks a Google account and approves the permissions behind those areas. Zernio saves the account and redirects to `redirect_url` (or the dashboard) with `connected=google`, `profileId`, `accountId` and `username` (the mailbox address).

## Choose the areas

`scopes` is required on Google. Pass a comma-separated subset of these areas:

| Area | Google scope | What it unlocks |
|------|--------------|-----------------|
| `email` | `gmail.modify` | Read, search, send, reply, label and trash Gmail messages |
| `calendar` | `calendar` | Read and write events, RSVP, free-busy and availability |
| `files` | `drive.file` | Files Zernio created and files the user picked. See [Files](/platforms/google/files) |
| `contacts` | `contacts.readonly` | A read-only sync of Google Contacts into `/v1/contacts` |
| `directory` | `directory.readonly`, `contacts.other.readonly` | [People search](/platforms/google/contacts#search-people) across the Workspace directory and the people the user has emailed |

Every connection also asks for `openid`, `userinfo.email` and `userinfo.profile`, to identify the mailbox.

Ask only for what your product uses. Google's consent screen lists each permission, and Workspace admins review apps by what they request.

## Add areas later

To add an area, connect the same Google account again with the new list. Google keeps the areas granted earlier, and Zernio updates the same account, so its `accountId` does not change.

Areas only accumulate. Leaving one out of a reconnect does not remove it. To drop an area, disconnect the account and connect it again with the areas you want.

Pass `reconnectAccountId` to make sure the reconnect lands on a given account (Zernio then also asks again for the areas that account holds).

Also pass `loginHint` with the mailbox address to preselect it in Google's account picker:

```bash
curl "https://zernio.com/api/v1/connect/google?profileId=PROFILE_ID&scopes=email,calendar,files&loginHint=ana@example.com&reconnectAccountId=ACCOUNT_ID" \
  -H "Authorization: Bearer $ZERNIO_API_KEY"
```

## Connect from the dashboard

In the dashboard, the Google tile opens a chooser with the four areas, all ticked for a new account and the granted ones for a reconnect.

The consent screen then asks only for the ticked areas.

From the API the flow has no Zernio screen: your app chooses the areas, and the user sees only Google's account picker and consent screen.

## How it behaves

- One Google account is one Zernio account per profile. Connecting it again updates that account instead of adding a second one.
- A new Google account is asked for exactly the areas in `scopes`, whatever other Google accounts on the profile hold.
- Zernio records the areas Google actually granted. If the user unticks a permission on Google's consent screen, that area is left out of the account and calls in it answer `412` `account_scope_missing`.
- Access renews by itself. When Google answers a renewal with `invalid_grant` (the user removed Zernio from their Google account, or the grant expired), Zernio asks once more and then marks the account disconnected: `isActive: false`, `needsReconnection: true`, [account health](/accounts/get-account-health) reports `error`, and `account.disconnected` fires once with `reasonCode: "auth_failed"` and `reconnectUrl`, a hosted link valid for seven days. A timeout, a throttle (`429`) or a Google outage never disconnects an account. Reconnecting the same Google account reactivates it with the same `accountId` and clears the flags.
- After connecting, Zernio subscribes to push notifications for the granted areas, and a `contacts` connection starts a contact sync. See [Webhooks](/platforms/google/webhooks).

## If it fails

| Error | Cause | Fix |
|-------|-------|-----|
| `400` `invalid_field_value`, `param: scopes` | `scopes` is missing, empty or has an unknown area | Pass a subset of `email`, `calendar`, `files`, `contacts`. |
| `400` on `loginHint` | The value is not an email address | Pass the mailbox address, or leave it out. |
| `error=oauth_denied` on the redirect | The user declined, or Google refused the app. `platform_error` carries Google's code | Start the connect again. `admin_policy_enforced` means the Workspace admin blocks the app: ask them to allow Zernio. |
| `error=invalid_state` on the redirect | The link expired or was already used | Request a fresh `authUrl`. |
| `412` `account_scope_missing` on a later call | The area was not granted | Connect again with the area in `scopes`. |

## Related

- [Start OAuth](/connect/get-connect-url)
- [Connecting accounts](/guides/connecting-accounts)
- [Get account health](/accounts/get-account-health)
- [Limits & errors](/platforms/google/reference)