# Google Drive and Gmail The site keeps files in one Google **shared drive**: the Request form (`/request`) puts what people send in its "Requests" folder, and files uploaded in Tools → Media → Files, for pages to link to, go in its "Media" folder (see "Files for pages" below). New requests can be emailed to whoever is listed in Panel → Site settings → Notices. Both go through one Google **service account** (`server/src/googleService.js`). Nothing here is needed for the rest of the site; until it's set up, the form takes no files and no mail goes out. | Setting on the `ngu-api` service | What it is | | --- | --- | | `GOOGLE_SERVICE_ACCOUNT` | Path to the service account's key file, eg. `/etc/ngu/google-service-account.json` | | `GOOGLE_SHARED_DRIVE` | The shared drive's **id only**, eg. `0ACUt32ze-CVGUk9PVA`: never the whole address (see step 2) | | `GOOGLE_MAIL_FROM` | The Workspace address notices are sent from, eg. `web@nextgenerationofunity.org` | Files need the first two; mail needs the first and third. ## 1. The service account 1. In Google Cloud Console (the project that holds the Google sign-in client is fine), enable the **Google Drive API** and the **Gmail API**. 2. IAM & Admin → Service accounts → Create. Name it eg. `ngu-site`. It needs no roles on the project. 3. Open it → Keys → Add key → JSON. Copy the file to the VPS, outside the app and the repo: ```sh sudo mkdir -p /etc/ngu sudo mv ngu-site-*.json /etc/ngu/google-service-account.json sudo chown : /etc/ngu/google-service-account.json sudo chmod 600 /etc/ngu/google-service-account.json ``` Note the account's email (`ngu-site@.iam.gserviceaccount.com`) and, from its Details page, its **Client ID** (a long number). ## 2. The shared drive A service account has no Drive storage of its own, so everything goes in a **shared drive**. 1. In Google Drive, make (or pick) a shared drive, eg. "NGU Website". 2. Shared drive → Manage members → add the service account's email as **Content manager** (it makes folders and deletes files). 3. Open the shared drive itself (not a folder in it). Its address is `https://drive.google.com/drive/u/2/folders/0ACUt32ze-CVGUk9PVA`; the id is only the part after `folders/`, here `0ACUt32ze-CVGUk9PVA`. A shared drive's id starts with `0A`. Leave out the address, the `/u/2/` part and anything after a `?`: pasting the whole URL gives `404 File not found` on every upload. ### Folders in it The site finds each folder it uses by name, at the top of the shared drive, and makes it the first time it's needed. Nothing needs making by hand. | Folder | Used by | Set in | | --- | --- | --- | | `Requests/` | Files sent with the Request form, a folder per type (`Requests/Chapters`, `Requests/Blog posts`...) | `DRIVE_FOLDER` and `TYPE_FOLDERS` in `server/src/routes/requests.js` | | `Media` and folders in it | Files for pages to link to (Tools → Media → Files), in folders you make there | `FOLDER` in `server/src/routes/files.js` | A new use calls `driveUpload("", name, mime, bytes)` from `server/src/googleService.js`; a path such as `"Media/2026"` makes each level. Add it to this table. Files for pages went in a folder called `Blog` before v1.9.10. If the shared drive has one, rename it to `Media` in Drive: the files inside keep working and stay listed, and nothing is made twice. Folders are matched by name, so renaming or moving one in Drive makes the site start a new one under the old name (files already stored keep working: they're found by their own id). People can add their own folders to the drive; the site ignores them. Request files are named `Request 12 - original name.docx`, so the number matches the one in Forms → Content requests. Deleting a request in the admin deletes its files from Drive too. Request files from before the per-type folders stay where they are, in `Requests` itself. **Deleting means Drive's trash.** Whatever the admin deletes (a request's files, a file, an empty folder) goes to the shared drive's trash, which keeps it 30 days; restore it from there if needed. A shared drive's Content manager, the service account's role, can trash but not permanently delete. ## 3. Gmail (domain-wide delegation) For the service account to send as a Workspace user: 1. Google Admin console → Security → Access and data control → API controls → **Manage domain wide delegation** → Add new. 2. Client ID: the service account's Client ID from step 1. OAuth scopes: `https://www.googleapis.com/auth/gmail.send` (and, for access groups, `https://www.googleapis.com/auth/admin.directory.group.member.readonly`, comma separated: `docs/google-sign-in.md`) 3. `GOOGLE_MAIL_FROM` is the address it sends as. It must be a **user** in the Workspace, one that signs in and has Gmail: not a group (eg. `webb-admin@`, even mail-enabled), not an alias. The service account acts as that user, and a group can't be acted as; Google answers `Client is unauthorized to retrieve access tokens`. A dedicated user such as `web@` keeps the sent copies out of anyone's own mailbox. A group is fine as a **recipient** (below): each member gets the notice. Then in the admin, Panel → Site settings → Notices → "Email new requests to": one or more addresses, separated by commas. Each new content request and data request sends one plain-text email with a link to the queue; replying goes to the person who sent the request. **Send a test notice**, under that box, emails whatever addresses are in it (saved or not) straight away and shows the result on the page: where it was sent from, or Google's reason for refusing. A new delegation can take a while to start working, so retry it after a few minutes before changing anything. ## 4. The service and nginx ```sh sudo systemctl edit ngu-api ``` ```ini [Service] Environment=GOOGLE_SERVICE_ACCOUNT=/etc/ngu/google-service-account.json Environment=GOOGLE_SHARED_DRIVE= Environment=GOOGLE_MAIL_FROM=web@nextgenerationofunity.org ``` nginx caps `/api/` bodies at 64k, so the form needs its own block in the site's `server` block, beside `/api/admin/import/docx` (`nginx-dev` has it): ```nginx location = /api/requests { proxy_pass http://127.0.0.1:3001; # the usual proxy_set_header lines proxy_read_timeout 120s; client_max_body_size 31m; } # Tools → Media → Files: one file of up to 50 MB location = /api/admin/files { proxy_pass http://127.0.0.1:3001; # the usual proxy_set_header lines proxy_read_timeout 180s; client_max_body_size 51m; } ``` `sudo nginx -t && sudo systemctl reload nginx && sudo systemctl restart ngu-api`. ## Checking it - Mail: Panel → Site settings → Notices → **Send a test notice**. - Send a request with a small file at `/request`. The journal shows `feedback #N (content: new blog, 1 files)`, and with mail on, `feedback #N: notice sent to 1`. - The file is in the shared drive's Requests folder (made on the first upload), and downloads from Forms → Content requests. - `Client is unauthorized to retrieve access tokens` (or `unauthorized_client`) on mail: `GOOGLE_MAIL_FROM` is a group or alias rather than a user, or the delegation (step 3) is missing, has the wrong Client ID (the service account's numeric Unique ID), or a scope other than exactly `https://www.googleapis.com/auth/gmail.send`, or is too new to have taken effect. `404 File not found` on upload means `GOOGLE_SHARED_DRIVE` isn't the bare id (a whole URL, say) or the service account isn't a member of the shared drive. ## What the form takes Up to 3 files of 10 MB each: Word documents (.docx), PDFs, JPEG and PNG, checked by their contents rather than their names (`server/src/routes/requests.js`). A Word document on a request can be turned into an unpublished blog post from the queue ("Make a blog post from it"), the same conversion as Blog posts → Import from Word (`docs/importing.md`). ## Files for pages Tools → Media → **Files** (editors and up upload, admins and up delete) puts a file in the shared drive's `Media` folder and shares it as **anyone with the link can view**, so visitors open it in Drive's viewer without signing in. Content block links pick from it with **Choose a file**: paragraph, button and image links, list and link-button items, and link cards. A link card also takes the file's name as its title and Drive's picture of the first page as its picture; the card says "Google Drive" above the title. - Types: PDF, Word, Excel and PowerPoint (.docx, .xlsx, .pptx), MP3, M4A and zip, up to 50 MB. The name and the contents must agree (a PDF named `.mp3` is refused). Older Office formats (.doc, .ppt) aren't taken: save them in the new format first. - The first-page picture: Drive draws one for PDFs and Office files a little after upload. The server asks at once and again over the next few minutes, and saves it into the image library as an ordinary upload. Audio and zips get an icon in the admin and no picture. - **Files put in the Media folder in Drive itself** (dragged in, made there, moved there) are listed too: opening the Files tab or a "Choose a file" picker looks in the folder (at most every 30 seconds), adds anything new, shares it the same way and fetches its preview. Google's own Docs, Sheets and Slides count; folders inside it and shortcuts don't. A file Drive won't share with anyone who has the link is named in a red note instead of listed, and tried again every ten minutes. - **Folders:** Media → Files has a tab per folder under `Media` (up to three deep), and uploads go into the one showing. **New folder** makes one inside it, **Rename** renames it (and everything in it follows), and **Delete** (admins and up) removes it once it's empty. Each file has a Folder dropdown to move it. Choose a file has the same tabs. A folder made in Drive itself, and a file moved there by hand, show up on the next look. - Deleting a file in the admin moves it to Drive's trash; the delete says first which records still link to it (those links stop working). Deleting it in Drive by hand leaves a broken link and a list entry that can still be deleted. - **"Too large for the web server (HTTP 413)"** on upload (older builds said "Server did not return JSON", with nothing in the journal): nginx refused the file before it reached the API, because the site has no `location = /api/admin/files` block (above), so the 64k `/api/` limit applies. `sudo tail /var/log/nginx/error.log` shows "client intended to send too large body". **Sharing has to be allowed.** Uploads fail with "Drive wouldn't share it with anyone who has the link" unless both allow it: 1. Admin console → Apps → Google Workspace → Drive and Docs → Sharing settings: sharing outside nextgenerationofunity.org is on (at least for the organizational unit the service account's delegated user is in, and for shared drives: "Allow people outside ... to access files in shared drives"). 2. The shared drive itself → Shared drive settings: "Allow people who aren't shared drive members to access files" and sharing outside the organization are on. Files the Request form stores are never shared this way; only files uploaded in Tools → Media → Files are.