# Put your bookings on Google Calendar

The Google Calendar integration copies each new PiGi booking onto one Google calendar and keeps that event up to date when the booking changes. You need your own Google Cloud project with an OAuth client (Client ID and Client Secret), and the Google account that owns the calendar.

Source: https://help.pigi.io/guides/integrations/scheduling/google-calendar

## Quick answers

**What does the Google Calendar integration actually do?**
It copies each new PiGi booking onto one Google calendar as an event, within about a
minute, and updates that event when the booking changes. It works in one direction
only: PiGi to Google. Moving or deleting the event in Google does not change the booking
in PiGi.

**Do I need my own Google Cloud project?**
Yes. Each school creates its own OAuth client in the Google Cloud console and pastes its
**OAuth Client ID** and **OAuth Client Secret** into PiGi. PiGi does not supply a shared
Google app for calendar sync.

**What redirect URI do I put on the Google OAuth client?**
`https://api.pigi.io/api/v1/calendar/public/oauth/callback`, exactly as written, with no
trailing slash. Google compares it as text, so any difference fails with
`redirect_uri_mismatch`.

**Will my existing bookings appear on the calendar?**
No. Only bookings created after you connect are added. The **Sync now** button does not
copy older bookings across yet.

**Does "Keep in sync both ways" bring calendar changes back into PiGi?**
Not yet. Whichever option you pick, changes flow from PiGi to Google only. Edit the
booking in PiGi, not the event in Google.

**After signing in with Google I landed on Company Details. Did it work?**
Probably. PiGi returns you to Company Details rather than the Google Calendar page. Open
**Settings → Communication → Google Calendar**: if the card reads "Connected to Google
Calendar", it worked.

**Bookings stopped appearing after about a week. Why?**
Your OAuth consent screen is most likely still in **Testing**. Google issues refresh
tokens that expire after 7 days to External apps in Testing. Publish the app (set it to
In production), then press **Disconnect** in PiGi and connect again.

**Can each instructor have their own calendar?**
No. One company connects one Google account and picks one calendar for all bookings.

## What this does

Google Calendar sync puts the day's bookings in a calendar your staff already use. PiGi
signs in to Google with an OAuth client that belongs to your school, created in your own
Google Cloud project. For Google's terms and any charges, see the Google Cloud console.
Setup is split: the client credentials go on the Integrations screen, and the
Google account and calendar are chosen on the Google Calendar page.

## Before you start

- The **admin** role in PiGi.
- A Google account that owns, or can edit, the calendar bookings should go into.
- Access to the Google Cloud console (console.cloud.google.com) with that or another
  Google account.

## Set up Google Cloud

New to the Google Cloud console? [Get Google OAuth credentials](google-oauth-client.md) walks through every
screen, including Google Workspace (Internal) setups. In short:

1. In the Google Cloud console, create a project (or pick an existing one).
2. Go to **APIs & Services → Library**, find **Google Calendar API** and click **Enable**.
3. Go to **Google Auth platform → Branding** and fill in the app name and support email.
   Under **Audience**, choose **Internal** if your calendar account is in your own Google
   Workspace organisation, otherwise **External**.
4. For an External app, change its publishing status on the **Audience** page from
   Testing to In production. Google will show an "unverified app" warning when you sign in; for your own
   account you can continue past it.
5. Go to **Google Auth platform → Clients → Create Client**, choose **Web application**,
   and under **Authorized redirect URIs** click **Add URI** and enter
   `https://api.pigi.io/api/v1/calendar/public/oauth/callback`. Click **Create**.
6. Copy the client ID and client secret Google shows you.

## Connect it to PiGi

1. Open **Settings → Communication → Integrations** and click the **Google Calendar**
   tile, then **Connect Google Calendar**.
2. Click **Add credentials**, paste the **OAuth Client ID** and **OAuth Client Secret**,
   and press **Save**. Leave **Webhook Verification Token (Optional)** empty.
3. Click **Google account & calendar** (or open **Settings → Communication → Google
   Calendar**).
4. Press **Connect Google Calendar** and sign in with the Google account that owns the
   calendar. Allow the calendar permissions.
5. You return to PiGi on Company Details. Open **Settings → Communication → Google
   Calendar** again.
6. Under **What to sync**, choose **Which calendar should bookings go into?** Your
   primary calendar is marked "(main)".
7. Leave **Keep bookings in sync automatically** on and press **Save changes**.

## Test it

1. On the Integrations screen, open **Google Calendar** and click **Test connection**. A
   working setup reports "Authorised. Bookings sync to …" with the calendar name.
2. Create a test booking in PiGi for a date in the next few days.
3. Within a minute or two, check the chosen Google calendar for the event. Change the
   booking time in PiGi and confirm the event moves.

## Troubleshooting

- **Google shows `redirect_uri_mismatch`.** The URI on the OAuth client is not exactly
  `https://api.pigi.io/api/v1/calendar/public/oauth/callback`. Fix it in Google Cloud and
  connect again.
- **"A bit of setup is still needed".** The Client ID and Client Secret are not saved.
  Click **Open integration settings** and add them.
- **"Google Calendar isn't enabled yet".** Connect the Google Calendar tile on the
  Integrations screen first.
- **"No Google account is authorised yet."** (Test connection.) The client is saved, but
  nobody has signed in with Google. Use **Connect Google Calendar**.
- **"Google no longer accepts the stored authorisation (invalid_grant)."** Access was
  revoked, the app is in Testing, or the client secret changed. Press **Disconnect** and
  connect again.
- **"We couldn't load your Google calendars".** Same causes as above. Disconnect and
  reconnect.

## Known limitations

- PiGi to Google only. Calendar edits are not read back, whichever sync option is chosen.
- **Sync now** and **Only pull changes from calendar** do not change anything yet, and
  report "0 bookings synced".
- Bookings created before you connected are not added to the calendar.
- One Google calendar per company.
- After Google sign-in you land on Company Details, with no confirmation message.

## Sources

- [Google: Create access credentials](https://developers.google.com/workspace/guides/create-credentials)
- [Google: Configure the OAuth consent screen](https://developers.google.com/workspace/guides/configure-oauth-consent)
- [Google: Enable Google Workspace APIs](https://developers.google.com/workspace/guides/enable-apis)
- [Google: Using OAuth 2.0 (refresh token expiration)](https://developers.google.com/identity/protocols/oauth2)
- [Google Cloud: Unverified apps](https://support.google.com/cloud/answer/7454865)
