Using JWT flow in salesforce - Both side configuarations in salesfroce - target ,source org configarations
Intigration scenario : Using JWT flow in salesforce - comm org button click need to authorise vsr org ang send some data get back response
Note : comm = org1 , vsr = org2
Step1: Create self sighned
cert in comm(org1)
stepup ->
Certificate and Key Management -> click button "Create Self-Signed
Certificate"
-> give name "JWT_VSR_CERT" click "Save"
2048,3072 cert last 1 year expiry , 4096 cert will expiry in 2 years.
down load this certificate "JWT_VSR_CERT"
Step2: (org2) goto vsr org -> stup -> Manage External
Client Apps -> new ->
External Client App Name : CommunicationsCloud
Contact Email : siva@gmail.com
OAuth Settings -> Enable check box
Callback URL : https://communicationscloudsiva.my.salesforce.com/
Selected OAuth Scopes : Manage user
data via APIs (api), Perform requests at any time (refresh_token,
offline_access)
Enable JWT Bearer Flow : checkbox check
upload cert
downloaded from comm
security :
Require Proof Key for Code Exchange (PKCE) extension for Supported
Authorization Flows : disable this check box
click save
Step3: (org2) Tab : Policies
Start Page : OAuth
OAuth Start URL :
https://communicationscloudsiva.my.salesforce.com
IP Relaxation : Enforce IP restrictions, but relax for
refresh tokens
Step4 : (org2) Tab : Settings
click on
"Consumer Key and Secret" -> redirect to url in browser ask
verification code from email which you gave on above "siva@gmail.com"
fill code then it will show
https://vsrsupremesolutions.my.salesforce.com/
Consumer Key
3MVG9GBhY6wQjl2sZRydHD.ZuVRPAOvdlBNEyl.DEfCtEJpKz6fdjRXPKFUiRpok4PGElm7tlEcdvVVyN_pz_
Consumer Secret
EB0A2DBF272395D6B571C667968BB79E250F3329765E78268EB2FEB9A6BA488E
Step5 : (org1) Goto comm org -> setup -> Named
Credentials -> External Credential tab -> new
External
Credential Configuration (Communication Org)
Field Value
Authentication
Protocol OAuth 2.0
Authentication
Flow Type JWT Bearer Flow
Identity
Provider URL : https://login.salesforce.com/services/oauth2/token
(assuming VSR is Production)
Issuer
(iss) 3MVG9GBhY6wQjl2sZRydHD.ZuVRPAOvdlBNEyl.DEfCtEJpKz6fdjRXPKFUiRpok4PGElm7tlEcdvVVyN_pz_ (Consumer Key)
Subject
(sub) siva@vsrsupremesolutions.in
Audience
(aud) https://login.salesforce.com
JWT
Expiration 300
Signing
Certificate JWT_VSR_CERT
Signing
Algorithm RS256
Step6: (org1) Scroll down
on External Credential Configuration”
you fill find section
"Principals" click on new
VSR_Principal
Step7: (org1)Create permission set : “VSR API Access”
Goto ->
External Credential Principal Access -> “VSR_JWT_EC - VSR_Principal” give permission to this one
Step8: (org1) assign
permission set (VSR_API_Access) to user able to use callout to vsr org
Step9: (org1) Create named
credential
Label :
VSR API
Name :
VSR_API
URL :
https://vsrsupremesolutions.my.salesforce.com
Enabled
for Callouts : True
External
Credential : VSR JWT External Credential
Client
Certificate : JWT_VSR_CERT
Generate
Authorization Header : True
step10 : (org1) Create apec class VSRApiService
public with sharing
class VSRApiService {
public static
String getAccounts() {
HttpRequest
req = new HttpRequest();
req.setEndpoint(
'callout:VSR_API/services/data/v65.0/query?q=' +
EncodingUtil.urlEncode(
'SELECT Id, Name FROM Account LIMIT 10',
'UTF-8'
)
);
req.setMethod('GET');
req.setHeader('Content-Type','application/json');
Http http =
new Http();
HttpResponse
res = http.send(req);
if(res.getStatusCode() == 200){
return
res.getBody();
}
throw new
CalloutException(
'Error : '
+ res.getStatusCode() +
' Body : '
+ res.getBody()
);
}
}
Step11: (org1) Run Anonimous apex execution window
String response = VSRApiService.getAccounts();
System.debug(response);
Comments
Post a Comment