SparkWA uses official Google Gmail APIs with least-privilege OAuth 2.0 scopes. It does not use deprecated Out-Of-Band (OOB) authentication, does not store Google account passwords, and never exposes public HTTP listeners.
1. Google Cloud Console Setup#
Step 1: Create or Select a GCP Project#
- Go to the Google Cloud Console.
- Click the project dropdown at the top and click New Project.
- Name it
SparkWA-Bridgeand click Create.
Step 2: Enable the Gmail API#
- In the search bar at the top, type
Gmail APIand select it from Marketplace. - Click Enable.
Step 3: Configure the OAuth Consent Screen#
- In the left navigation menu, go to APIs & Services > OAuth consent screen.
- Select External (or Internal if using Google Workspace organization) and click Create.
- Fill in the required fields:
- App name:
SparkWA - User support email: Your personal email.
- Developer contact information: Your personal email.
- Click Save and Continue.
- On the Scopes page, click Add or Remove Scopes.
Add the following minimum scopes:
https://www.googleapis.com/auth/gmail.send(Send messages on your behalf)https://www.googleapis.com/auth/gmail.readonly(Read messages and drafts)
- Click Save and Continue.
- On the Test Users page, click + Add Users and add:
- Your
BRIDGE_GMAIL_ADDRESS - Your personal email
- Click Save and Continue.
[!IMPORTANT]
Testing Mode Note: In Google Cloud's "Testing" status, OAuth refresh tokens expire after 7 days unless your GCP project is moved to "In Production" status. To avoid re-authorizing weekly, click Publish App on the OAuth Consent Screen page (even without full verification, published personal apps with personal scopes retain persistent refresh tokens).
Step 4: Create OAuth 2.0 Client Credentials#
- Go to APIs & Services > Credentials.
- Click + Create Credentials > OAuth client ID.
- Select Application type:
- Choose Desktop app (recommended for loopback redirect) OR Web application.
- If choosing Web application, add Authorized redirect URI:
http://127.0.0.1:8085/oauth/callback
- Click Create.
- Click Download JSON on the confirmation dialog.
- Rename the downloaded file to
credentials.json.
2. Deploying Credentials to VPS#
Copy credentials.json from your development machine to your VPS:
scp credentials.json user@your-vps-ip:/tmp/credentials.jsonOn the VPS, move it to the protected configuration directory:
sudo mkdir -p /etc/sparkwa
sudo mv /tmp/credentials.json /etc/sparkwa/credentials.json
sudo chown root:sparkwa /etc/sparkwa/credentials.json
sudo chmod 0600 /etc/sparkwa/credentials.json3. Running Headless OAuth Authorization#
Because your Ubuntu VPS has no graphical interface or browser, SparkWA provides a secure loopback helper that coordinates with your desktop browser.
Method 1: SSH Port Forwarding (Recommended)#
- Connect to your VPS with port 8085 forwarded to your local machine:
ssh -L 8085:127.0.0.1:8085 user@your-vps-ip- Run the authorization command on the VPS:
sudo -u sparkwa sparkwa gmail-auth --config /etc/sparkwa/config.env- SparkWA will print an authorization link:
Open this authorization URL in your browser:
https://accounts.google.com/o/oauth2/auth?...- Copy the link and open it in your desktop browser.
- Log in with your Bridge Gmail Account (
BRIDGE_GMAIL_ADDRESS), review permissions, and click Allow. - Google will redirect to
http://127.0.0.1:8085/oauth/callback. Because port 8085 is forwarded via SSH, the request reaches your VPS loopback server automatically! - The terminal will display:
✓ Received authorization code via loopback callback!
✓ Successfully saved OAuth token with refresh capabilities to /var/lib/sparkwa/token.json (permissions 0600)
✓ Successfully verified Gmail API connection for: bot-bridge@gmail.comMethod 2: Manual Terminal Paste Fallback#
If you cannot forward ports via SSH:
- Open the URL printed by
sparkwa gmail-authin your desktop browser. - After clicking "Allow", your browser will attempt to open
http://127.0.0.1:8085/oauth/callback?state=...&code=4/0A.... - Even though the browser shows "Connection Refused", simply copy the entire URL from the browser's address bar.
- Paste that URL directly into the terminal prompt:
Auth Code or Redirect URL: http://127.0.0.1:8085/oauth/callback?code=4/0A...- SparkWA extracts the code and exchanges it for the token automatically.
4. Token Security#
- Stored at:
/var/lib/sparkwa/token.json - Permissions:
0600(readable only by thesparkwaservice account). - SparkWA automatically uses the
refresh_tokento renew expired access tokens silently in the background.