Private Google Sheets access (service account)
The scheduled importer can read a Google Sheet without making it public. Instead of "anyone with the link", the spreadsheet is shared with a service account: a Google identity that belongs to a Cloud project and has no password or mailbox. Jethro signs a request with the account's JSON key and reads the sheet through the Google Sheets API.
This page sets that up once per Jethro installation. The importer side is described in Importing Persons and the developer reference.
What you need
- A Google account that can create a Cloud project, or an existing project you may use.
- The spreadsheet, and permission to share it (Owner or Editor).
- Shell access to the Jethro server that runs the cron job.
The service account only needs read access to that one spreadsheet. It does not need an IAM role, and it never needs access to the rest of your Google Workspace.
1. Create or choose a Cloud project
Open the Cloud Console project page and create a project, for example
jethro-sheet-import. If your organisation already has a suitable project, select it from the
project picker instead. Note the project ID; the key you create later belongs to this project.
2. Enable the Google Sheets API
Open the Google Sheets API page with the right project selected and click Enable. Without this, every request fails with HTTP 403 even when the spreadsheet is shared correctly.
3. Create the service account
- Open Service accounts.
- Click Create service account.
- Name it, for example
jethro-sheet-import. The display name is for humans; the generated email address (ending iniam.gserviceaccount.com) is the identity you share the sheet with. - Skip the optional role steps: no project role is needed.
- Click Done.
4. Download a JSON key
-
Click the new service account, then the Keys tab.
-
Add key → Create new key → JSON → Create.
-
The browser downloads a
.jsonfile. This is the only copy Google will give you; treat it like a password. -
Copy it to the Jethro server, outside the web root, for example:
sudo install -o www-data -g www-data -m 600 \~/Downloads/jethro-sheet-import-*.json \/etc/jethro/google-sheets-service-account.jsonUse the user that runs the cron job. Never commit the key, and do not place it inside the Jethro checkout: the importer warns if the path is under the checkout root.
5. Share the spreadsheet with the service account
- Open the spreadsheet, click Share.
- Paste the service account's email address (
jethro-sheet-import@<project>.iam.gserviceaccount.com). - Choose Viewer.
- Untick Notify people (the service account has no inbox), then Share.
Do not turn on "Anyone with the link". The sheet can stay restricted to you, your team, and this one service account.
If your Google Workspace policy blocks sharing with accounts outside your organisation, ask your Workspace administrator, or create the Cloud project inside the same organisation.
6. Run the importer
Use the normal share/edit URL and name the tab explicitly; naming the tab avoids relying on the "last tab" default:
php scripts/import_persons.php \
--google-spreadsheet='https://docs.google.com/spreadsheets/d/<spreadsheet-id>/edit' \
--google-spreadsheet-tab=import \
--google-service-account=/etc/jethro/google-sheets-service-account.json \
--group=42 \
--match-existing \
--dry-run
Check the reported counts, then run the same command without --dry-run. The first run needs
a target group: create one with --create-group='Signup import', note its ID, and use
--group=<id> on subsequent runs so every run adds to the same audit group.
Setup for cron is the same command; the script exits non-zero on failure, so cron mail or your job runner reports it:
*/15 * * * * cd /var/www/jethro && php scripts/import_persons.php \
--google-spreadsheet='https://docs.google.com/spreadsheets/d/<spreadsheet-id>/edit' \
--google-spreadsheet-tab=import \
--google-service-account=/etc/jethro/google-sheets-service-account.json \
--group=42 --match-existing >> /var/log/jethro/import.log 2>&1
Using a key from the Admin import page
The same key works for a one-off import without cron. Open Admin → Import, choose From Google Spreadsheet, paste the share link, optionally name the tab, and upload the JSON key. The key is read for that request and is not stored; the sheet must still be shared with the service account as Viewer. For recurring imports, keep the key on the server and use the scheduled command above.
Troubleshooting
| Message | Likely cause |
|---|---|
HTTP 403 / PERMISSION_DENIED / "caller does not have permission" | The sheet is not shared with the service account, or the Sheets API is not enabled in the project that owns the key. |
HTTP 401 / invalid_grant / Invalid JWT Signature | The key was deleted or replaced, the service account is disabled, or the server clock is far out. |
HTTP 404 | Wrong spreadsheet ID, or the tab name does not exist. |
The sheet tab "…" is empty | The tab has no data rows. |
Published Google Sheets links cannot be used with a service account | Use the normal share/edit URL, not a /d/e/…/pubhtml link. |
Warning: the service-account key is inside the Jethro checkout | Move the key outside the checkout and update the command. |
No sheet has the gid … | The #gid= belongs to another spreadsheet; check the URL. |
Security notes
- The importer requests only
https://www.googleapis.com/auth/spreadsheets.readonly. The service account can read the tabs shared with it and nothing else. - The key file is a long-lived credential. Keep it outside the web root, readable only by the cron user, and out of repository backups that are broadly accessible.
- To revoke access, remove the service account from the spreadsheet's Share list, or delete the key in the Cloud console. Deleting the service account stops every copy of the key.
- One key per installation makes rotation and revocation straightforward. Rotate by creating a new key, updating the server, and deleting the old key.
- The importer never prints the spreadsheet URL, the key, or the access token.
Alternative: user OAuth
An OAuth refresh token for a human Google account also works with the Sheets API, but it is a long-lived credential tied to a person, survives password changes, and grants whatever that person can read. Prefer the service account unless your Workspace policy prevents creating one.