Sub topic 3
Setting up a connected app
This chapter assumes you know basic HTTP, can read JSON and short Python snippets, have administrator access to a Salesforce org (Developer Edition or sandbox), and know the idea of a public/private key pair. It covers the one-time configuration that every flow in this tutorial depends on.
What a connected app is
A connected app is the record in Salesforce that represents your external application. It holds the identity and rules Salesforce uses when your application asks for a token:
- A consumer key, which is the public client ID. It is sent as
client_idin OAuth requests. - A consumer secret, which is the confidential password for the app. It is sent as
client_secretwhere a flow needs it. - The callback URL, the exact address Salesforce may redirect the browser to after login.
- The OAuth scopes the app is allowed to request.
- Policies that decide who may use the app and how long tokens last.
- An optional certificate, used to verify JWTs your server signs.
Salesforce also offers External Client Apps, a newer packaging model for the same job. The concepts in this chapter (key, secret, callback URL, scopes, policies) carry over, though the setup screens differ. The steps below use the connected app screens in Setup.
Which settings each flow needs
All three flows share the same record, but each reads different parts of it.
| Setting | Web server flow | JWT bearer flow | Client credentials flow |
|---|---|---|---|
| Callback URL | Required, must match exactly | Required by the form, not used | Required by the form, not used |
| Scopes | `api`, plus `refresh_token` if needed | `api`, plus `refresh_token` if needed | `api` |
| Digital signature (certificate) | Not used | Required | Not used |
| Consumer secret | Sent at token request | Not sent | Sent at token request |
| Client Credentials Flow checkbox and run-as user | Not used | Not used | Required |
| Permitted Users policy | Usually "All users may self-authorize" | "Admin approved users are pre-authorized" | Not applicable, the run-as user is fixed |
Creating the app
- In Setup, search for App Manager and select New Connected App.
- Enter a name, an API name, and a contact email.
- Under API (Enable OAuth Settings), check Enable OAuth Settings.
- Enter the Callback URL. For the examples in this tutorial, use
https://app.example.com/oauth/callback. - Add the scopes you need to Selected OAuth Scopes. The next chapter explains how to choose them.
- Enable the options that match your flow, as shown below.
- Save. New connected apps can take a few minutes before Salesforce accepts their credentials.
- Open the app from App Manager, choose View, and select Manage Consumer Details to read the consumer key and secret. Salesforce may ask you to verify your identity first.
Options for the web server flow
Enter an HTTPS callback URL that your server controls. For local testing, Salesforce accepts http://localhost callbacks. Select the refresh_token scope if the app must keep working after the access token expires. To use PKCE, leave the option that requires a proof key for code exchange enabled where it is offered. Chapter material on state and PKCE comes later, but the setting lives here.
Options for the JWT bearer flow
Check Use digital signatures and upload the certificate (server.crt) that matches the private key on your integration server. Salesforce verifies the signature of each JWT against this certificate. You will create the key pair in a later chapter. The upload step is part of connected app setup, so plan for it.
Options for the client credentials flow
Check Enable Client Credentials Flow. Salesforce shows a warning that the flow acts as a single user. Then open Manage for the app, choose Edit Policies, and set Run As to the user whose permissions the tokens should carry. Without a run-as user, token requests fail.
Policies
Policies are on the app's Manage page under Edit Policies. They apply after the OAuth settings and control who can get a token.
- Permitted Users has two common values. All users may self-authorize lets any user in the org approve the app at login. Admin approved users are pre-authorized allows only users whose profile or permission set has been assigned to the app. The JWT bearer flow needs the second value, because no one is present to click an approval screen.
- IP Relaxation controls whether a login from an unfamiliar IP address needs extra verification. Server integrations often use Relax IP restrictions or have their IP ranges listed on the app, so token requests from a data center are not blocked.
- Refresh Token Policy sets whether refresh tokens last until revoked or expire after a time or period of inactivity.
- Session Timeout sets how long an access token's session is valid.
The profile and permission set assignment for pre-authorization is covered in its own chapter.
How the pieces fit together
Checking your setup
Before writing code, confirm that the app is configured by sending the authorization URL for the web server flow to a browser. A correctly registered app shows the Salesforce login page. A wrong consumer key or callback URL shows an error page instead.
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>Replace <CONSUMER_KEY> with the value from Manage Consumer Details. Use https://test.salesforce.com as the host for a sandbox. The callback does not need to be reachable for this check, because the error or login page appears before the redirect.
Common setup mistakes
- Using the app too soon. A new app can reject its credentials for a few minutes. Wait and retry before changing settings.
- Callback mismatch. Copying the URL from a different environment (staging instead of production) is the usual cause.
- Missing scope. A token only carries scopes the app lists. Adding a scope later requires users to authorize again.
- Wrong policy for JWT. With self-authorization left on, a JWT request fails with
invalid_grantbecause the user has not approved the app. - No run-as user. The client credentials flow fails until a user is chosen under Edit Policies.
- Secret in source control. Treat the consumer secret like a password. Keep it in an environment variable or secrets manager.
Exercise
You configured a connected app for the JWT bearer flow and uploaded the certificate. Token requests still fail with invalid_grant, and the app's Permitted Users policy is "All users may self-authorize". What should you change, and why?
Show answer
Set Permitted Users to "Admin approved users are pre-authorized", then assign the integration user's profile or a permission set to the app. The JWT bearer flow has no interactive login, so the user must be approved in advance. With self-authorization, the user has never approved the app, and Salesforce rejects the assertion.
Exercise
Your web server flow shows redirect_uri_mismatch. The app registers https://app.example.com/oauth/callback, and your code sends https://app.example.com/oauth/callback/. Why does this fail, and how do you fix it?
Show answer
Salesforce compares the callback URL as an exact string, and the trailing slash makes the two values differ. Either send the registered value without the slash, or register the URL with the slash and send that same value everywhere, including the token request.