# Get Google OAuth credentials (Client ID and secret)

Google Calendar and Google Analytics 4 in PiGi sign in to Google with an OAuth client that belongs to your school. This guide walks you through the Google Cloud console from scratch: create a project, turn on the API, set up Google Auth Platform (Branding, Audience, Data access, Clients), add PiGi's exact redirect URI and copy the Client ID and Client secret into PiGi.

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

## Quick answers

**What is an OAuth client and why does PiGi need mine?**
An OAuth client is a Client ID and Client secret you create in the Google Cloud console.
It lets your school's Google account give PiGi permission to write calendar events
(Google Calendar) or read reports (Google Analytics 4). PiGi does not supply a shared
Google app for these, so each school creates its own.

**Can one OAuth client serve both Google Calendar and Google Analytics?**
Yes. Use one Google Cloud project and one Web application client, enable both APIs, add
both redirect URIs to the client, and paste the same Client ID and Client secret into
both tiles in PiGi.

**What redirect URI do I add for Google Calendar?**
`https://api.pigi.io/api/v1/calendar/public/oauth/callback`, exactly as written, with no
trailing slash. PiGi does not show this one on screen, so copy it from here.

**What redirect URI do I add for Google Analytics 4?**
`https://api.pigi.io/api/v1/core/google-analytics/callback`. PiGi also shows it at the
bottom of the **Google authorisation** panel on the Google Analytics 4 tile; copy it from
there if the two ever differ.

**Which scopes do I add under Data access?**
For Google Calendar: `https://www.googleapis.com/auth/calendar` and
`https://www.googleapis.com/auth/calendar.events`. For Google Analytics 4:
`https://www.googleapis.com/auth/analytics.readonly`. Add only the ones for the
integrations you use.

**We use Google Workspace. Should I pick Internal or External?**
Internal, if the person who will connect PiGi signs in with an address in your Workspace
organisation. Internal apps need no Google verification, show no unverified-app warning
and are not limited to 7-day test access. Only people in your organisation can connect,
which is fine: your own calendar owner is the one who signs in.

**I have a personal Gmail account. Can I choose Internal?**
No. Internal needs a Google Workspace (or Cloud Identity) organisation. Choose External
and publish the app to In production.

**Why does my calendar disconnect every week?**
Your app is External and still in **Testing**. Google expires access granted to a
Testing app seven days after consent. On **Audience**, click **Publish app**, then
**Disconnect** in PiGi and connect again.

**Do I need Google to verify my app?**
Not for a school connecting its own account. Google does not require verification for
Internal apps, or for personal-use apps with fewer than 100 users. An unverified External
app shows a warning screen when you sign in; you can continue past it for your own
account.

**I lost the client secret. Can I see it again?**
No. Google shows the full secret only when it is created, then only its last four
characters. Open the client, click **Add secret**, paste the new secret into PiGi, then
disable the old one.

**My Google Workspace admin blocks third-party apps. What do they change?**
In the Admin console, under **Security → Access and data control → API controls → Manage
App Access**, they click **Configure new app**, search for your Client ID and set it to
**Trusted**.

**Will my OAuth client stop working if I leave it alone?**
Google deletes OAuth clients that have been inactive for six months, and emails a warning
30 days before. A connected calendar keeps it in use.

## What this does

Google Calendar and Google Analytics 4 in PiGi both ask for an **OAuth client ID** and
**OAuth client secret** from your own Google Cloud project. Google uses them to show your
staff a consent screen and hand PiGi a lasting permission. Creating a project and OAuth
client is free; see the Google Cloud console for Google's terms.

## Before you start

- The **admin** role in PiGi.
- The Google account that owns the calendar or Analytics property. For Google Workspace,
  sign in to the Cloud console with your Workspace address so the project belongs to your
  organisation.
- About 20 minutes.

## Set up Google Cloud

1. Go to console.cloud.google.com and sign in. Open **Menu → IAM & Admin → Create a
   Project**, enter a **Project name** (for example "PiGi bookings"), check **Location**
   shows your organisation (Workspace) and click **Create**.
2. Open **Menu → APIs & Services → Library**. Search for **Google Calendar API** and
   click **Enable**. For Google Analytics 4, also enable **Google Analytics Data API**.
3. Open **Menu → Google Auth Platform → Branding** (this area was called the "OAuth
   consent screen"). Enter an **App name** such as your school name, choose a **User
   support email**, and continue.
4. Under **Audience**, choose **Internal** for a Google Workspace account, otherwise
   **External**. Enter a contact email address, accept the Google API Services User Data
   Policy and finish the wizard.
5. Open **Data access → Add or remove scopes** and add the scopes PiGi asks for (listed
   in Quick answers). Save.
6. External only: on **Audience**, either click **Publish app** to move from Testing to
   In production (recommended), or, while still testing, add the connecting Google
   account under **Test users**. Access granted in Testing expires after seven days.
7. Open **Clients → Create client**. Set **Application type** to **Web application** and
   give it a name.
8. Under **Authorized redirect URIs**, click **Add URI** and paste each PiGi redirect URI
   you need. It must match character for character: `https`, no trailing slash, no
   spaces. Leave **Authorized JavaScript origins** empty.
9. Click **Create**. Copy the **Client ID** and **Client secret** straight away, or
   download the JSON. Google will not show the full secret again.

## Connect it to PiGi

1. Open **Settings → Communication → Integrations** and click the **Google Calendar** or
   **Google Analytics 4** tile, then **Connect Google Calendar** or **Connect Google
   Analytics 4** if it is not connected yet.
2. Click **Add credentials**. Paste the Client ID into **OAuth Client ID** (Calendar) or
   **OAuth client ID** (Google Analytics 4), and the secret into **OAuth Client Secret**
   or **OAuth client secret**. Press **Save**.
3. Sign in to Google:
   - Google Calendar: click **Google account & calendar**, then **Connect Google
     Calendar**.
   - Google Analytics 4: in the **Google authorisation** panel, click **Connect Google**.
4. Choose the Google account, continue past the unverified-app screen if shown, and allow
   the permissions.

## Test it

- Google Calendar: open the tile and click **Test connection**. "Authorised. Bookings
  sync to …" means the client and sign-in work.
- Google Analytics 4: the **Google authorisation** panel shows **Authorised**.

## Troubleshooting

- **`redirect_uri_mismatch`** (Google's error page): the URI on the client differs from
  PiGi's. Correct it under **Clients**, save, wait a few minutes and try again.
- **`invalid_client`**: the Client ID pasted into PiGi is wrong or the client was
  deleted. Copy it again from **Clients**.
- **`access_denied`**: you clicked cancel, the account is not a test user while the app
  is in Testing, or a Workspace admin blocks the app. Fix the cause and connect again.
- **Unverified-app warning**: expected for an External app you have not sent for
  verification. Continue for your own account.
- **Wrong or replaced secret**: Google refuses the sign-in at the last step. Add a new
  secret and paste it into PiGi.
- **Connection stops after about a week**: the app is External and in Testing. Publish
  it and reconnect.
- **"Google did not return a refresh token…"** (Google Analytics 4): remove the app from
  your Google account's third-party access, then connect again.

## Known limitations

- PiGi shows the Google Analytics 4 redirect URI but not the Google Calendar one.
- After a Google Calendar sign-in, success or failure, you land on Company Details with no
  message. Check **Settings → Communication → Google Calendar** for "Connected to Google
  Calendar".

## Sources

- [Google: Create a Google Cloud project](https://developers.google.com/workspace/guides/create-project)
- [Google: Enable Google Workspace APIs](https://developers.google.com/workspace/guides/enable-apis)
- [Google: Configure the OAuth consent screen and choose scopes](https://developers.google.com/workspace/guides/configure-oauth-consent)
- [Google: Create access credentials](https://developers.google.com/workspace/guides/create-credentials)
- [Google Cloud Help: Manage OAuth clients](https://support.google.com/cloud/answer/15549257)
- [Google Cloud Help: Manage app audience](https://support.google.com/cloud/answer/15549945)
- [Google Cloud Help: When is verification not needed](https://support.google.com/cloud/answer/13464323)
- [Google Cloud Help: Unverified apps](https://support.google.com/cloud/answer/7454865)
- [Google: Sensitive scope verification](https://developers.google.com/identity/protocols/oauth2/production-readiness/sensitive-scope-verification)
- [Google: Using OAuth 2.0 (refresh token expiration)](https://developers.google.com/identity/protocols/oauth2)
- [Google: OAuth 2.0 scopes for Google APIs](https://developers.google.com/identity/protocols/oauth2/scopes)
- [Google Workspace Admin Help: Control which apps access Google Workspace data](https://knowledge.workspace.google.com/admin/apps/control-which-apps-access-google-workspace-data)
