gildas-lormeau/SingleFile · error · Error
code_required
code_required
Error message
code_required
What it means
initAuth throws "code_required" when the auth flow ended without producing an authorization code. The library caught the underlying flow error, found no captured code (the local `code` variable is still unset because extractAuthCode never resolved), and re-throws with the original error as `cause`. It signals that a valid OAuth authorization code must be supplied before auth can complete.
Source
Thrown at src/lib/gdrive/gdrive.js:315
}
options.code = searchParams.get("code");
return await authFromCode(gdrive, options);
} else if (options.launchWebAuthFlow) {
options.extractAuthCode(browser.identity.getRedirectURL(), authFlow)
.then(authCode => code = authCode)
.catch(() => { /* ignored */ });
return await options.launchWebAuthFlow({ url: gdrive.authURL }, authFlow);
} else {
throw new Error("auth_not_supported");
}
}
catch (error) {
if (error.message && (error.message == "code_required" || error.message.includes("access"))) {
if (code) {
options.code = code;
return await authFromCode(gdrive, options);
} else {
throw new Error("code_required", { cause: error });
}
} else {
throw error;
}
}
finally {
if (authFlow.cancel) {
authFlow.cancel();
}
}
}
function generateState() {
return Array.from(crypto.getRandomValues(new Uint8Array(16)))
.map(value => value.toString(16).padStart(2, "0"))
.join("");
}
View on GitHub (pinned to 517fb7c5cf)
Solutions
- Inspect error.cause to learn why the flow failed (redirect URI mismatch, user cancellation, network).
- Ensure options.extractAuthCode resolves with the auth code from the redirect URL before the flow promise settles.
- Retry the auth flow with interactive: true so the user can complete consent.
- As a fallback, obtain an auth code out-of-band and pass options.code so initAuth calls authFromCode directly.
Example fix
// before
const token = await gdrive.auth({ launchWebAuthFlow }); // user closed popup -> code_required
// after
try {
const token = await gdrive.auth({ launchWebAuthFlow });
} catch (e) {
if (e.message === "code_required") {
const token = await gdrive.auth({ code: promptForAuthCode(gdrive.authURL) });
}
} Defensive patterns
Strategy: try-catch
Try / catch
try { await gdrive.auth(options); }
catch (e) {
if (e.message === "code_required") {
const cause = e.cause; // inspect why the flow failed
// prompt user again or fall back to manual code entry
await gdrive.auth({ ...options, code: await obtainCodeManually() });
} else throw e;
} Prevention
- Make the auth flow interactive so users can complete consent
- Register the exact redirect URI in the Google OAuth client settings
- Always set options.code in options before initiating the flow so partial results can be reused
When it happens
Trigger: launchWebAuthFlow fails or is closed by the user; extractAuthCode's promise rejects (its .catch swallows the error, leaving `code` undefined); the inner flow throws an error whose message is "code_required" or contains "access", and options.code was not set.
Common situations: User cancels the Google consent popup; the redirect URL is not registered in the Google Cloud OAuth client settings; network failure during the consent flow; passing a stale/expired code while extractAuthCode also fails.
Related errors
AI-assisted analysis of gildas-lormeau/SingleFile@517fb7c5cf (2026-09-01).
Data as JSON: /api/errors/fabbae057fa7e5f7.
Report an issue: GitHub.