Sub topic 1
OAuth 2.0 Fundamentals in Salesforce
Prerequisites
Assume you know basic Salesforce administration, HTTP requests, REST APIs, and the difference between authentication and authorization.
OAuth 2.0 and Connected Apps
OAuth 2.0 is a standard that lets an external application request limited access to Salesforce without receiving a user's password. Salesforce returns an access token. The application sends that token in the Authorization: Bearer header when it calls an API.
A connected app is a Salesforce configuration that defines how an external application connects. It stores the application's OAuth settings, allowed scopes, callback URLs, and access policies. Salesforce uses these settings when it issues tokens.
A newer external client app separates developer settings from administrator policies and is the recommended model for new integrations. Existing integrations commonly still use connected apps.
OAuth Settings
The consumer key identifies the application. A consumer secret proves the application's identity, so store it in a secret manager and never expose it in browser code.
The callback URL, also called the redirect URI, is where Salesforce sends the user after authorization. Salesforce compares the requested URL with the configured value. A mismatch prevents the authorization request from completing.
#!/usr/bin/env bash
set -eu
: "${CONSUMER_KEY:?Set CONSUMER_KEY first}"
REDIRECT_URI="https://app.example.com/callback"
AUTH_URL="https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=${CONSUMER_KEY}&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&scope=api%20refresh_token"
printf '%s\n' "$AUTH_URL"Choosing Scopes
A scope limits what an access token can do. Request the smallest set that supports the integration.
| Scope | Typical use |
|---|---|
| `api` | Call Salesforce APIs |
| `refresh_token` | Obtain a new access token later |
| `openid` | Use OpenID Connect identity information |
Choosing an OAuth Flow
Choose a flow based on whether a person signs in and where secrets can be stored.
| Situation | Suitable flow |
|---|---|
| A user authorizes a web or mobile application | Authorization code with PKCE |
| A trusted server runs as a specific Salesforce user | JWT bearer |
| A trusted service runs as a configured Salesforce user | Client credentials |
PKCE protects the authorization code exchange when the client cannot safely keep a secret. JWT bearer and client credentials avoid interactive login, but administrators must control the Salesforce user and permissions used by the integration.
Exercise
Which setting must exactly match the URL that receives the authorization response?
Show answer
The callback URL, also called the redirect URI.