NGU-Web/docs/google-drive-and-mail.md
Zaldimmar 8fd3a9b15b Add a test notice button to Site settings
Panel → Site settings → Notices → Send a test notice emails the
addresses in the box now and shows where it came from, or Google's
reason for refusing, so the Gmail setup can be checked without the
journal. Superadmins only (POST /api/admin/panel/test-notice).

The docs now say the sender must be a Workspace user: a group can't
be acted as through delegation, though it can receive notices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 17:18:43 -05:00

6.7 KiB

Google Drive and Gmail (the Request form)

The site keeps files in one Google shared drive: the Request form (/request) puts what people send in its "Requests" folder, and later uses (eg. files linked from blog posts) get folders of their own. 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:

    sudo mkdir -p /etc/ngu
    sudo mv ngu-site-*.json /etc/ngu/google-service-account.json
    sudo chown <ngu-api user>: /etc/ngu/google-service-account.json
    sudo chmod 600 /etc/ngu/google-service-account.json
    

    Note the account's email (ngu-site@<project>.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 DRIVE_FOLDER in server/src/routes/requests.js

A new use calls driveUpload("<Folder>", name, mime, bytes) from server/src/googleService.js; a path such as "Blog/2026" makes each level. Add it to this table.

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.

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
  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

sudo systemctl edit ngu-api
[Service]
Environment=GOOGLE_SERVICE_ACCOUNT=/etc/ngu/google-service-account.json
Environment=GOOGLE_SHARED_DRIVE=<shared drive id, eg. 0ACUt32ze-CVGUk9PVA>
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):

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;
}

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).