Sub topic 2
Authorization and token endpoints, and why My Domain matters
This chapter assumes you know basic HTTP (methods, headers, form-encoded bodies, redirects) and can read a curl command. You also need the OAuth 2.0 roles and tokens from the previous chapter: client, resource owner, authorization server, access token, and refresh token.
The two endpoints
Salesforce exposes its OAuth 2.0 authorization server under /services/oauth2/. Two endpoints matter for the flows in this tutorial.
| Endpoint | Path | Used by | Purpose |
|---|---|---|---|
| Authorization | `/services/oauth2/authorize` | Web server flow | The browser is sent here so the user can log in and approve access. |
| Token | `/services/oauth2/token` | All three flows | The client posts a grant and receives an access token. |
The authorization endpoint is only for flows that involve a person at a browser. The JWT bearer and client credentials flows skip it and go straight to the token endpoint, because no user is present to approve anything.
Choosing the host
Both paths are appended to a base host. Which host you use depends on the org type and on whether My Domain is in play.
| Host | When to use it |
|---|---|
| `https://login.salesforce.com` | Production and Developer Edition orgs, for flows that allow the generic login host. |
| `https://test.salesforce.com` | Sandboxes. |
| `https://MyDomainName.my.salesforce.com` | Your org's My Domain URL. Required for the client credentials flow. |
My Domain is a name you register for your org, which gives it its own login host. A request sent to that host is already tied to one org.
Why My Domain matters
The generic hosts do not know which org a request is for. They work for flows where the request carries an identity, such as a username in a JWT or a user who types a username at the login page.
The client credentials flow carries no username. The consumer key and secret are the only identifiers, and the connected app they belong to lives in one specific org. The request therefore has to go to that org's My Domain URL. If you post it to login.salesforce.com, it fails.
My Domain has two other practical effects:
- It lets you customize login behavior for that org, and some features require it.
- It gives you a stable host to put in configuration. You still read
instance_urlfrom the token response to decide where to send API calls.
Seeing the endpoints in a request
The token endpoint always receives an HTTP POST with a form-encoded body. Only grant_type and the credentials change from flow to flow. This is the client credentials request, sent to a My Domain host:
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/token \
-d grant_type=client_credentials \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET>Compare it with the web server flow, which begins with a browser redirect to the authorization endpoint:
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=S256Later chapters explain each parameter. For now, notice that one is a browser URL and the other is a server-to-server POST.
Common mistakes
- Sending the client credentials request to
login.salesforce.cominstead of the My Domain URL. - Using the production host for a sandbox connected app, or the reverse. The app exists in only one org.
- Sending the token request as JSON. The endpoint expects form-encoded parameters.
Exercise
Why can the JWT bearer flow use a generic login host, while the client credentials flow needs the My Domain URL?
Show answer
A JWT assertion names the user (the sub claim) and the connected app (the iss claim), so Salesforce can identify the org. Client credentials requests carry only a consumer key and secret and no username, so they must be sent to the My Domain URL to reach the right org.