Salesforce's Mohith Shrivastava's second Connected App tutorial shows how the JWT Bearer Token Flow can be used with Postman to obtain access tokens. Separately, SFDCStop shows how to use an access token to retrieve data from Salesforce. Summary of the process for getting an access token via JWT Bearer Token flow is given below, along with the alternative Username-Password flow.
Mohith's second tutorial largely follows the Trailblazer documentation for creating the JWT necessary for obtaining an access token. First, within Postman, open a POST request to https://test.salesforce.com/services/oauth2/token
(or login.salesforce.com for production) with two query attributes, grant_type
hardcoded to urn:ietf:params:oauth:grant-type:jwt-bearer
and assertion
with a value that can be generated at JWT.io as follows:
Place {"alg":"RS256"}
in the JWT header.
Place the following in the JWT payload, updating the iss
value with the Client ID of the connected app, the aud
value (should be either https://test.salesforce.com
for sandbox instances, https://login.salesforce.com
for all others, including developer instances), the sub
with the Salesforce resource owner (the user ID, usually expressed as an email), and exp
value being the ten-digit current UNIX timestamp. Ensure there are no spaces or carriage returns in the payload:
{"iss":"--client id--","sub":"--salesforce user--","aud":"https://test.salesforce.com","exp":"1616865336"}
You may also wish to add a "jti" field (JWT ID), the value of which can be a randomly generated UUID. As stated in the Trailblazer documentation linked above, if a "jti" field is added, Salesforce will make sure the JWT hasn't been used to make a prior access token request, serving as a guard against replay attacks. If it has been used, it will return a 400 response code with message { "error": "invalid_request", "error_description": "jti already in use"}
.
Finally, in the Verify Signature portion, as generated in the previous tutorial, place the PublicCert.crt contents and the private key found in key.pem (including the BEGIN/END delimiters) in the corresponding boxes. JWT.io should report "Signature Verified", upon which you can copy the left-side as the assertion value
into the Postman call. Make the call and you should get an access token in response.
This process is much more straightforward as no keys are used. As shown in the Salesforce documentation, you can simply make a POST request with the following five query parameters: grant_type, client_id, client_secret, username, and password.
As demo'ed in the SFDCStop video, can create a GET request to say https://yoursalesforceinstance.salesforce.com/services/data/v50.0/query?q=SELECT+Name,Type+FROM+Account
. Under the Authorization tab, select "Bearer", copy-and-paste the access token in, and make the API call.
More information on SOQL calls are available here.
Posted by Glen Mazza in Salesforce CRM at 03:00AM Mar 28, 2021 | Tags: salesforce postman oauth2 | Comments[0]