Sub topic 2
Authorization and token endpoints, and why My Domain matters
This chapter assumes you know the OAuth 2.0 roles (client, resource owner, authorization server, resource server) and the difference between an authorization code, an access token, and a refresh token. It also assumes you can read a curl command and a short Python script. Here you learn which Salesforce URLs each flow talks to and how to choose the host part of those URLs.
The two endpoints
Salesforce exposes OAuth through a small set of paths under /services/oauth2/. Two of them carry nearly every flow in this tutorial.
| Endpoint | Path | Method | Who calls it | Used by |
|---|---|---|---|---|
| Authorization | `/services/oauth2/authorize` | GET (browser redirect) | The user's browser | Web server flow |
| Token | `/services/oauth2/token` | POST (form-encoded) | Your server | All three flows |
The authorization endpoint is interactive. Your app redirects the user's browser to it, the user logs in and approves access, and Salesforce redirects back to your callback URL with a code parameter. Your server never calls this endpoint directly.
The token endpoint is not interactive. Your server sends an HTTP POST with Content-Type: application/x-www-form-urlencoded, and Salesforce answers with JSON. A common mistake is sending a JSON body. Salesforce expects form fields such as grant_type, client_id, and client_secret, which is why the curl -d key=value examples work.
The JWT bearer and client credentials flows skip the authorization endpoint completely. They only use the token endpoint, and they differ in the grant_type and the other fields they send.
Other endpoints under the same path appear later in the tutorial:
/services/oauth2/revokeends a token's validity. See the chapter on revoking tokens./services/oauth2/userinforeturns identity details for the token's user./services/oauth2/introspectreports whether a token is active.
Choosing the host: login URL versus My Domain
The path is fixed. The host in front of it is the part you must choose, and you have three common options.
| Host | Typical use |
|---|---|
| `https://login.salesforce.com` | Production and Developer Edition orgs, using the generic login URL |
| `https://test.salesforce.com` | Sandbox orgs, using the generic login URL |
| `https://MyDomainName.my.salesforce.com` | Your org's own My Domain URL |
A sandbox My Domain URL has the form https://MyDomainName--SandboxName.sandbox.my.salesforce.com. If your users sign in through an Experience Cloud site, the site's own URL can serve as the OAuth host instead, and the token is then issued for that site.
The generic hosts work for the web server flow and the JWT bearer flow because those flows identify the user, and Salesforce can find the org from the user. The client credentials flow has no user in the request. Salesforce can only find the org from the host name, so you must send the request to your My Domain URL.
Why My Domain matters
My Domain gives your org a unique host name. This has several practical effects:
- Client credentials needs it. A request to
login.salesforce.comhas no way to say which org's connected app owns the consumer key. Posting to your My Domain URL answers that question. - It is the stable base for your integration. The generic login hosts are shared by all orgs, while your My Domain URL belongs only to you.
- Policy can be enforced. My Domain lets an org control login behavior for its own host, for example which login methods are offered on the login page.
- Sandbox refreshes are predictable. A sandbox keeps its own My Domain URL, so you can pin each environment's host in configuration.
A flow that works against login.salesforce.com in a Developer Edition org may therefore fail the moment you switch to client credentials. The usual cause is that the request still goes to the generic host.
Keep the same host for one flow
Within one flow, send the authorization request and the token request to the same host. If you send the user to login.salesforce.com/services/oauth2/authorize and then redeem the code at MyDomainName.my.salesforce.com/services/oauth2/token, expect failures such as invalid_grant. Put the host in one configuration value and build both URLs from it.
The aud claim of a JWT bearer assertion follows the same rule. It must be the login host that matches the org type: https://login.salesforce.com for production, https://test.salesforce.com for a sandbox, or your My Domain URL. An assertion with a mismatched audience is rejected.
The host in the token response is not the host you called
After a successful token request, Salesforce returns an instance_url. This is the host that serves the REST API for that org. You send API calls there, not to the login URL.
Token request -> login.salesforce.com or MyDomainName.my.salesforce.com
API calls -> instance_url from the token responseNever hard-code the instance host. Orgs can move between instances, and the value in the response is the correct one. The next chapters cover the response fields in detail.
Discovering endpoints programmatically
Salesforce publishes an OpenID Connect discovery document on every host. It lists the endpoint URLs for that host, so you do not have to assemble them by hand.
curl https://login.salesforce.com/.well-known/openid-configurationThe following script reads the document and prints the main URLs. It uses only the requests package.
import requests
HOST = "https://login.salesforce.com" # or your My Domain URL
resp = requests.get(f"{HOST}/.well-known/openid-configuration", timeout=10)
resp.raise_for_status()
config = resp.json()
for key in ("issuer", "authorization_endpoint", "token_endpoint",
"revocation_endpoint", "userinfo_endpoint"):
print(f"{key}: {config.get(key)}")Run it against both a generic host and your My Domain URL and compare the output. Note that the issuer and endpoint values reflect the host you asked.
Common mistakes with hosts
- Sandbox against
login.salesforce.com. The user or connected app is not found, and the error looks like an invalid grant or client ID. Usetest.salesforce.comor the sandbox My Domain URL. - Client credentials against a generic host. The request fails because the org cannot be identified. Use the My Domain URL.
- Mixing hosts between authorize and token. Use one host per flow.
- API calls sent to the login host. Use
instance_url. - Missing HTTPS or a trailing slash in the base URL. Salesforce endpoints require HTTPS. Build URLs with a single
/between host and path.
Exercise
Your client credentials request to https://login.salesforce.com/services/oauth2/token fails, although the consumer key and secret are correct. What do you change, and why?
Show answer
Send the request to the org's My Domain URL, for example https://MyDomainName.my.salesforce.com/services/oauth2/token. The client credentials request contains no user, so Salesforce can only identify the org, and its connected app, from the host name.
Exercise
After a successful token request you receive an instance_url. Which host should receive your REST API calls, and why not the host you sent the token request to?
Show answer
Send API calls to the instanceurl host. The login or My Domain host handles authentication, while instanceurl is the host Salesforce reports as serving the REST API for that org, and it can change, so you should not hard-code it.