Sub topic 7
Web server flow: acting on behalf of a user
This chapter assumes you know basic HTTP (redirects, form-encoded bodies, headers), can run curl, and have a connected app with a callback URL and the api and refresh_token scopes enabled. It also assumes you know the token response fields from the earlier chapters.
What the flow does
The web server flow is the OAuth 2.0 authorization code grant. Use it when a server-side application must act on behalf of a person. The user logs in to Salesforce directly. Your app never sees their password. It receives a short-lived authorization code and trades it for an access token.
The access token carries that user's permissions. API calls respect the user's profile, permission sets, and sharing rules.
Step 1: Send the user to authorize
Redirect the browser to the authorization endpoint. The parameters are explained after the URL.
https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<CONSUMER_KEY>&redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%2Fcallback&scope=api%20refresh_token&state=<RANDOM_STATE>&code_challenge=<S256_CHALLENGE>&code_challenge_method=S256response_type=codeselects the authorization code grant.client_idis the connected app's consumer key.redirect_urimust exactly match a callback URL registered on the connected app, and it must be URL-encoded.scopelists what the app asks for.refresh_tokenis required if you want a refresh token.stateis a random value you generate and store in the user's session. Check that it matches when the user returns.code_challengeandcode_challenge_methodadd PKCE, covered in the next chapter.
Step 2: Handle the callback
Salesforce redirects the browser to your callback URL with code and state query parameters. Compare state with the stored value. If they differ, stop and reject the request. This blocks forged callbacks.
Step 3: Exchange the code for tokens
The authorization code is single-use and expires quickly, so exchange it right away. Make this call from your server.
curl -X POST https://login.salesforce.com/services/oauth2/token \
-d grant_type=authorization_code \
-d code=<AUTHORIZATION_CODE> \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET> \
-d redirect_uri=https://app.example.com/oauth/callback \
-d code_verifier=<CODE_VERIFIER>The response contains access_token, instance_url, and, if the scope was granted, refresh_token. Send API calls to instance_url with the header Authorization: Bearer <access_token>.
Common mistakes
| Symptom | Likely cause |
|---|---|
| `redirect_uri_mismatch` | Callback URL is not registered, or differs in scheme, host, path, or trailing slash |
| `invalid_grant` | Code already used, expired, or sent to the wrong login host |
| `invalid_client_id` | Wrong consumer key, or the app is not in this org |
| No `refresh_token` returned | `refresh_token` scope was not requested or not enabled on the app |
Exercise
A user approves access and is redirected back to your callback, but your token request fails with invalid_grant. You are sure the consumer key and secret are correct. Name two likely causes and how to check each.
Show answer
The authorization code may have been used already or expired, so check whether the callback handler runs twice (for example on a page refresh) and exchange the code immediately. The redirect_uri or the login host may also differ from the authorization request, so compare them exactly. A sandbox login needs test.salesforce.com, not login.salesforce.com.