What PKCE adds, in three parameters
PKCE binds the authorization request to the token request so that an intercepted authorization code cannot be redeemed by anyone else. The client generates a high-entropy random string, the code_verifier, and keeps it. It sends only the SHA-256 hash of that string, base64url-encoded, as code_challenge on the authorize call, along with code_challenge_method=S256. At the token exchange it sends the original verifier, and Salesforce hashes it and compares.
That is the whole mechanism, and the two failures it produces are distinguishable. "Missing required code_challenge parameter" fires at the authorize step and means the app requires PKCE and the client sent none. "Invalid code_verifier" fires at the token step and means PKCE was sent but the verifier does not hash to the challenge — the client generated a new verifier instead of storing the one it used, or encoded it wrongly.
Generating the pair correctly
Two encoding details cause most "invalid code_verifier" failures. The challenge must be base64url — plus becomes minus, slash becomes underscore, and the trailing equals padding is stripped — not standard base64. And the hash is taken over the ASCII bytes of the verifier string itself, not over a decoded form of it.
The verifier must persist across the two requests, keyed to the OAuth state value, because the authorize call and the token call are separate HTTP transactions and frequently separate processes. Storing it in a variable that does not survive the redirect is the other common cause of an invalid verifier.
// Node — generate the pair
import crypto from "node:crypto";
const codeVerifier = crypto.randomBytes(64).toString("base64url"); // 43-128 chars
const codeChallenge = crypto
.createHash("sha256")
.update(codeVerifier) // ASCII bytes of the verifier string
.digest("base64url"); // base64url, NOT base64 — no +, /, or =
// Persist codeVerifier against the `state` value before redirecting.
// 1) Authorize — send the CHALLENGE
// https://login.salesforce.com/services/oauth2/authorize
// ?response_type=code&client_id=3MVG9...
// &redirect_uri=https://n8n.example.com/rest/oauth2-credential/callback
// &scope=api%20refresh_token%20offline_access
// &state=STATE
// &code_challenge=<codeChallenge>
// &code_challenge_method=S256
// 2) Token — send the VERIFIER (the original string, not the hash)
// POST https://login.salesforce.com/services/oauth2/token
// grant_type=authorization_code&code=ONE_TIME_CODE
// &client_id=...&client_secret=...
// &redirect_uri=https://n8n.example.com/rest/oauth2-credential/callback
// &code_verifier=<codeVerifier>
The usual cause is the wrong checklist for the app type
Most occurrences of this error are not a PKCE implementation problem at all. They come from creating one app type while following setup instructions written for the other. An External Client App should have Require PKCE enabled along with Require Secret for both the Web Server and Refresh Token flows. A legacy Connected App should have all three disabled.
Copying a legacy checklist onto a newly created External Client App leaves PKCE required while the client performs a plain code flow — producing exactly this error. Copying the current checklist onto a legacy app produces the mirror image, where the client sends PKCE parameters the app is not configured to handle.
Check the app type before touching the client. It is a two-second look in Setup and it decides which direction the fix goes.
Changing the PKCE setting takes several minutes to propagate, and a new app can take around ten minutes before it accepts any authorization. Retrying instantly after saving reproduces the error and sends you looking for a second cause that does not exist.
Isolating it in one request
Build the authorize URL by hand in a private browser window and try it twice: once without the PKCE parameters and once with them. If the bare URL fails and the PKCE version succeeds, the app requires PKCE and the client is not sending it — the fix is in the client or in the app setting, and you now know which direction. If both fail identically, PKCE is not the cause and the problem is scopes, permitted users, callback URL or propagation.
A private window matters here. A partially completed OAuth session in a normal browser makes results inconsistent between attempts, which is how a clean binary test turns into an afternoon.
In n8n specifically, check the credential type before the parameters. The built-in Salesforce OAuth2 credential handles PKCE according to how the app is configured, while a Generic OAuth2 credential requires you to supply the parameters yourself — so the same app can work with one credential type and fail with the other for reasons that have nothing to do with the app.
PKCE, refresh tokens and what happens after the first connection
PKCE applies to the authorization-code exchange, not to subsequent refreshes — once you hold a refresh token, renewing an access token is a straightforward token-endpoint call with no challenge or verifier involved. That is worth knowing because it changes where to look when a connection works for a day and then fails: a failure at refresh time is about the refresh token, the client secret or the app's Require Secret setting for the Refresh Token Flow, not about PKCE.
The Require Secret settings are configured separately for the Web Server Flow and the Refresh Token Flow, and setting one without the other produces precisely this pattern — an initial connection that succeeds and a refresh that fails hours later, which reads as an expiry problem and is a configuration one.
Make sure refresh_token and offline_access are in the requested scopes. Without them Salesforce issues an access token and no refresh token at all, so the integration works until that token expires and then fails permanently, with an error that mentions expiry and says nothing about the missing scope that caused it.
Where these facts come from