Gmail Add-on + Workspace Marketplace + Service Account (DWD)
DIY End-to-End Onboarding Guide
Tip: Keep a simple table of all IDs (Project Number, Script ID, Deployment ID, etc.) for quick reference.
Note: Make sure the projects are under the same name in appsscript and google cloud console
{
"timeZone": "Asia/Kolkata",
"runtimeVersion": "V8",
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/userinfo.email"
],
"urlFetchWhitelist": [
"https://tpir.threatcop.com/"
],
"gmail": {
"name": "TPIR Email Reporting",
"logoUrl": "https://tpir.threatcop.com/api/static/logo/favicon.png",
"contextualTriggers": [
{ "unconditional": {}, "onTriggerFunction": "getContextualAddOn" }
],
"primaryColor": "#0E2954",
"secondaryColor": "#4A9C29"
}
}
Note: If it is not visible yet, confirm App visibility is Private, same org, and wait a few minutes.
import { google } from 'googleapis';
import fs from 'fs';
const SCOPES = [
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/admin.directory.user.readonly',
'https://www.googleapis.com/auth/admin.directory.group.readonly'
];
export async function getClients(serviceAccountJsonPath, subjectUser) {
if (!fs.existsSync(serviceAccountJsonPath)) throw new Error('Missing service account JSON');
const auth = new google.auth.JWT({
keyFile: serviceAccountJsonPath,
scopes: SCOPES,
subject: subjectUser // user in your Workspace domain
});
await auth.authorize(); // fails if DWD or scopes are wrong
const gmail = google.gmail({ version: 'v1', auth });
const admin = google.admin({ version: 'directory_v1', auth });
return { gmail, admin };
}
ID / Value |
Where to Find |
Script ID |
Apps Script → Project settings → Script ID. |
Deployment ID |
Apps Script → Deploy → Manage deployments. |
Project Number |
Cloud Console → Dashboard → Project info. |
Service Account numeric Client ID |
Cloud Console → IAM & Admin → Service accounts → select account. |
DWD page |
Admin Console → Security → API controls → Manage domain-wide delegation. |
A) e.messageMetadata is undefined — Deploy and test in Gmail; do not run add‑on entrypoints from the editor.
B) Manage deployments spinner — Hard refresh, try incognito/another browser; console UI hiccup.
C) Add‑on not visible in Admin console — Ensure Private visibility, same org, and allow propagation time.
D) Two add‑ons appear — Remove older test deployment or uninstall from Admin console.
E) unauthorized_client on JWT — DWD missing/wrong Client ID or scopes don’t match code.
F) 403 accessNotConfigured — Enable Gmail API/Admin SDK in the service account's project; wait a few minutes.
G) redirect_uri_mismatch (only if making your own OAuth client for Script) — Use: https://script.google.com/macros/d/<YOUR_SCRIPT_ID>/usercallback