Originally published on ExtensionBooster.
Adding OAuth 2.0 to a Chrome extension trips up a lot of developers because the usual web redirect flow simply does not fit. This walkthrough covers the four setup steps, a small token helper you can drop into a Manifest V3 project, and the mistakes that cause most "it worked yesterday" auth bugs.
Why extensions can't use the normal redirect flow
A typical web app sends the user to Google, then Google redirects back to an HTTPS callback URL. Extension pages are not served over HTTPS, so that pattern breaks.
Chrome solves this with the Identity API (chrome.identity). It handles:
- the Google consent screen
- token caching between sessions
- silent sign-in for users who already granted access
- redirects, so you configure no redirect URI at all
Your extension never sees a password. It asks Chrome for an access token and sends that token to Google APIs such as Gmail, Drive, Calendar or Contacts.
The four setup steps
- Lock your extension ID. Upload the extension to the Chrome Developer Dashboard (no need to publish), copy the public key from the Package tab, and paste it into
manifest.jsonas thekeyfield. Reload the unpacked build and confirm onchrome://extensionsthat the ID matches the dashboard one. - Create OAuth credentials. In the Google API Console, pick a project, create an OAuth 2.0 Client ID of type Chrome Extension, and enter that extension ID. Enable every API you plan to call, and create an API key.
- Declare it in the manifest. Request the
identityandstoragepermissions and add anoauth2block. - Write the token logic. Get a token, call the API, recover from expiry, and support sign-out.
Here is a minimal manifest for step 3:
{ "manifest_version": 3, "name": "My Google-connected extension", "version": "1.0.0", "key": "YOUR_PUBLIC_KEY_FROM_THE_DASHBOARD", "permissions": ["identity", "storage"], "oauth2": { "client_id": "YOUR_CLIENT_ID.apps.googleusercontent.com", "scopes": ["https://www.googleapis.com/auth/userinfo.email"] } } Only ask for the scopes you need. Common ones cover basic profile, email, read-only contacts, Gmail, Drive, Calendar and Sheets. Swapping userinfo.email for contacts.readonly is the pattern Chrome's own OAuth tutorial uses for the People API.
A small token helper
In Manifest V3, chrome.identity.getAuthToken returns a promise that resolves to an object with a token field (per the Identity API reference). The helper below wraps that, retries once on a 401, and handles sign-out:
async function getToken(interactive = false) { const { token } = await chrome.identity.getAuthToken({ interactive }); if (!token) throw new Error('No token returned'); return token; } async function googleFetch(url, options = {}, retried = false) { const token = await getToken(true); const res = await fetch(url, { ...options, headers: { ...options.headers, Authorization: `Bearer ${token}` }, }); // Expired or revoked token: drop it from Chrome's cache and try once more if (res.status === 401 && !retried) { await chrome.identity.removeCachedAuthToken({ token }); return googleFetch(url, options, true); } return res; } async function signOut() { const token = await getToken(false).catch(() => null); if (!token) return; await fetch(`https://oauth2.googleapis.com/revoke?token=${token}`, { method: 'POST' }); await chrome.identity.removeCachedAuthToken({ token }); } The revoke endpoint is the one documented in Google's OAuth 2.0 guide. Call getToken(true) from a popup or options page button so a real user click starts the consent screen.
Pitfalls that waste the most time
| Symptom | Likely cause | Fix |
|---|---|---|
| "Invalid client" while testing unpacked | Unpacked ID differs from the dashboard ID | Add the key field to the manifest |
| API call returns 403 | API not enabled in the Cloud Console | Enable each API your scopes touch |
| Users suddenly see the consent screen again | You changed scopes after publishing | Plan your scopes before launch |
| Requests start failing after a while | Expired token not handled | Remove the cached token and retry |
| Interactive sign-in fails in the background | Service workers have no user gesture | Trigger interactive: true from the popup or options page |
Key takeaways
- Use
chrome.identityinstead of building your own redirect flow. - Pin your extension ID with the manifest
keybefore creating credentials. - Request the smallest set of scopes and settle them before you publish.
- Treat a 401 as "refresh the token", not as a fatal error.
- Start interactive sign-in from a user-facing page, never the service worker.
For the full step-by-step version with a complete popup example and a scope reference table, read the Chrome extension OAuth 2.0 Google authentication guide. If you are also working on getting your extension discovered and reviewed, ExtensionBooster's growth tools for extension developers are worth a look.
Which part of Google sign-in gave you the most trouble in your extension: the extension ID, scopes, or token expiry?