Newer Version Available
Authorize an Org Using the JWT Bearer Flow
The JWT bearer authorization flow requires a digital certificate, also called a digital signature, to sign the JWT request. You can use your own certificate or create a self-signed certificate using OpenSSL. With this flow, explicit user interaction isn’t required. However, this flow does require prior approval of the client app. See OAuth 2.0 JWT Bearer Flow for Server-to-Server Integration.
-
If you do not have your own private key and digital certificate, use OpenSSL to create the key and a self-signed certificate.
It is assumed in this task that your private key file is named server.key and your digital certificate is named server.crt.
-
Create a connected app, and configure it for
Salesforce DX.
This task includes uploading the server.crt digital certificate file. Make note of the consumer key when you save the connected app because you need it later.
-
If the org you are authorizing is not hosted on https://login.salesforce.com, update your project configuration file
(sfdx-project.json).
Set the sfdcLoginUrl parameter to the login URL. Examples of other login URLs are your custom subdomain or https://test.salesforce.com for sandboxes. For example:
1"sfdcLoginUrl": "https://test.salesforce.com"Alternatively, you can use the --instanceurl parameter of the auth:jwt:grant command to specify the URL. This value overrides the login URL you specified in the sfdx-project.json file. See the next step for an example.
-
Run the auth:jwt:grant CLI command.
Specify the client identifier from your connected app (also called the consumer key), the path to the private key file (server.key), and the JWT authentication username. When you authorize a Dev Hub org, set it as the default with the --setdefaultdevhubusername parameter. For example:
1sfdx auth:jwt:grant --clientid 04580y4051234051 \ 2--jwtkeyfile /Users/jdoe/JWT/server.key --username jdoe@acdxgs0hub.org \ 3--setdefaultdevhubusername --setalias my-hub-orgThis example shows how to use the --instanceurl parameter to specify an org hosted on https://test.salesforce.com rather than the default https://login.salesforce.com:1sfdx auth:jwt:grant --clientid 04580y4051234051 \ 2--jwtkeyfile /Users/jdoe/JWT/server.key --username jdoe@acdxgs0hub.org \ 3--instanceurl https://test.salesforce.comYou can authorize a scratch org using the same client identifier (consumer key) and private key file that you used to authorize its associated Dev Hub org. Set the --instanceurl parameter to https://test.salesforce.com and the --username parameter to the administrator user displayed after you create the scratch org.
Authorize a Scratch Org
- You previously authorized your Dev Hub org using the JWT bearer flow.
- The private key file you used when authorizing your Dev Hub org is accessible and located in /Users/jdoe/JWT/server.key.
- You’ve created a scratch org and have its administration user’s username, such as test-wvkpnfm5z113@example.com.
-
Copy the consumer key from the connected app that you created in your Dev Hub
org.
- Log in to your Dev Hub org.
- From Setup, enter App Manager in the Quick Find box to get to the Lightning Experience App Manager.
-
Locate the connected app in the apps list, then click
, and select View.
- In the API (Enable OAuth Settings) section, click Manage Consumer Details, then verify your identity.
- Copy the Consumer Key to your clipboard. The consumer key is a long string of numbers, letters, and characters, such as 3MVG9szVa2Rx_sqBb444p50Yj (example shortened for clarity.)
-
Run the auth:jwt:grant CLI command. The
--clientid and --jwtkeyfile parameter values are the same as when you ran the command to
authorize a Dev Hub org. Set --username to the
scratch org’s admin username and set --instanceurl
to https://test.salesforce.com. For
example:
1sfdx auth:jwt:grant --clientid 3MVG9szVa2Rx_sqBb444p50Yj \ 2--jwtkeyfile /Users/jdoe/JWT/server.key --username test-wvkpnfm5z113@example.com \ 3--instanceurl https://test.salesforce.comIf you get an error that the user is not approved, it means that the scratch org information has not yet been replicated to https://test.salesforce.com. Wait a short time and try again.