Sub topic 3
Setting up a connected app
This chapter assumes you know basic HTTP, can read short JSON and curl examples, and have administrator access to a Salesforce Developer Edition org or sandbox. It also assumes you know the roles in OAuth 2.0 from the earlier chapters.
What a connected app is
A connected app is the record in your org that represents an external application. It holds the consumer key, the consumer secret, the allowed callback URLs, the OAuth scopes, and the access policies. Every token request names a connected app through client_id. If no matching app exists, Salesforce answers with invalid_client_id.
Salesforce is also introducing External Client Apps as a newer packaging model for the same job. The settings you choose are similar, but the screens differ. This chapter uses the connected app names.
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), select Enable OAuth Settings.
- Set the Callback URL. The web server flow needs it. Other flows still require a value, so use a placeholder such as
https://app.example.com/oauth/callback. - Add the scopes the integration needs.
- Save, then wait a few minutes before testing, because new apps can take time to become active.
Open the app from Manage Consumer Details to read the Consumer Key (client_id) and Consumer Secret (client_secret).
Settings that depend on the flow
| Setting | Web server | JWT bearer | Client credentials |
|---|---|---|---|
| Callback URL | Must match exactly | Required but unused | Required but unused |
| Digital certificate | Not needed | Upload `server.crt` | Not needed |
| Client credentials flow option | Off | Off | On |
| Run-as user | Not used | Not used | Required |
| Permitted Users policy | Any | Admin approved users | Any |
The callback URL is compared character by character. A trailing slash or a different scheme causes redirect_uri_mismatch.
Policies
After saving, open Manage and then Edit Policies. Two settings matter most.
- Permitted Users controls who can authorize the app. All users may self-authorize suits the web server flow. Admin approved users are pre-authorized is required for the JWT bearer flow, and you then grant access through profiles or permission sets.
- IP Relaxation controls whether a login from an unfamiliar IP address needs extra verification. Unattended server flows usually need a relaxed setting or an allowed IP range.
Checking your setup
The following request tests only that the connected app exists. A wrong key returns invalid_client_id, and a wrong secret returns invalid_client. The grant type here is deliberately unsupported for this test, so the unsupported_grant_type or invalid_grant error shows that the key was accepted:
curl -X POST https://login.salesforce.com/services/oauth2/token \
-d grant_type=authorization_code \
-d code=invalid \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET> \
-d redirect_uri=https://app.example.com/oauth/callbackExercise
You set up a connected app for the JWT bearer flow, but the token request fails because the user has not approved the app. Which policy setting should you check, and how do you grant access?
Show answer
Check Permitted Users. Set it to "Admin approved users are pre-authorized", then assign the user's profile or a permission set to the connected app so the user is pre-authorized.