Hello Everyone,
After investing
lot of time, I was able to Integrate with my own sandbox( you can say one Salesforce Instance to Other Salesforce instance)
I had a scenario
where i had to call a rest method created and Exposed in my own sandbox. So I
started digging on this as you guys are now.
In order to
perform OAuth with salesforce you need to perform 3 important steps.
1.
Create Connected App
2.
Create Auth. Provider
3.
Create Named Credentials
Once you are authorized, you just need to write 5- 6 lines
of code to invoke your REST method.
So let’s gets started.
As you know Salesforce uses OAuth 2.0 now. So our job is to set up
OAuth.
Importantly, OAuth requires 4 important details and they
are,
Client ID, Secrete Key, User Name and Password.
So how do we get them?
Client ID and Secrete Key (AKA Client Secrete) can be
obtained from Creating a Connected app. But what about User Name and Password??
User Name and Password can be obtained using Named credentials. Let's see how.
Creating Connected App:
Setup èApps
èScroll Down to
Connected App section and Click New.
Fill the details. Refer Screenshots.
Pic 1
Pic 2
Run As User: Chose your Org’s system Admin (preferably)
/ the person whom you want to run the Exposed REST method. When you try to invoke REST method salesforce will run
the class with this user.
Scopes: Full Access (full)
Any field you don’t see in the screen shot, you can leave
them blank.
We will come back to CallBack URL in a while. Just update
Dummy value as “https://login.salesforce.com " for now and Click Save.
You will now see some page like this.
Pic 3
Now you got your Client Key and Secret. Well done. Let’s hit the road and find a way to use Username and
Password.
As I said Named Credentials are the way to do it.
In order to create Named Credentials, we need something
called Authentication Provider.
This Auth Provider will act as junction between Named
Credentials and Connected App.
Creating the Auth Provider:
Setup è
Auth.Providers è
Click New
Chose Provider Type as Salesforce (Since we are integrating
with Salesforce to Salesforce).
Name and URL Suffix: Give any suitable name as per your convenience.
Consumer Key: Created in Previous step (Creating a Connect App)
Refer the screenshot.
Consumer Secret: Same as above.
Execute Registration As: Give the Same user which you
gave in above step (Run As User in Connected App Section)
Once you click save you will see something like this.
Pic 4
Now you have got Call Back URL. (Do you remember we gave
dummy value somewhere before this step?) Now it’s time to copy the call back
URL and go back to connected app and edit and Update the Call Back URL .
Pic 5
As I mentioned Auth provider acts as junction between named
Credentials and Connected App. Lets see how.
In any REST web call we need to pass User Name, Password, and
Client ID and secrete in the request header for authentication.
Client Secret and Key are generated in Connected App and You
Updated the Connected App with Call Back URL generated in Auth Provider. So now
you see how Connected App and Auth Provider are linked.
In Short Call Back URL
generated in Auth provider is used in Connected App.
Now time to see how Auth Provider is linked with Named Credentials.
Creating Named Credentials:
Setup è
Named Credentials è
Click New
Setup as below.
Pic 6
Look Closely. You need to choose the Auth Provider which you
created in previous step .With this Named Credentials and Auth provider are tied together . This is how Auth provider acts as Junction between Named Cred and Connected App.
Note:
Start Authentication Flow on Save should be checked and put
the scope as full.
If you see the pic, the Authentication Status is “Pending”
Now
When you click save it will ask you for the username and password of your salesforce org. once you provide the Valid Username and Password, somehow salesforce will save this in the backend.
After you provide the valid username and Password you will
get something like this.
Allow It and You are Done. Authentication Status will be changed to Authenticated as “ Authenticated as yourUserName” .
Pic 8
Now you just need to refer in you controller.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:eBloger_NamedCred/services/apexrest/YourMethodNameWhichIsExposed?param1='+userToBeUpdated.ID+'¶m2='+accountIDtobeUpdated);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
req.setEndpoint('callout:eBloger_NamedCred/services/apexrest/YourMethodNameWhichIsExposed?param1='+userToBeUpdated.ID+'¶m2='+accountIDtobeUpdated);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
NOTE: Parma1 and Param2 and something my httpGet method is
expecting as a parameter. Otherwise it can be vomited or modified according to
your scenario.
Now it is time for me and you to go explore what Refresh
Token full means and things which you are not clear from this article.
Even I am new-Bee to this integration stuff and trying to
help who is in my position J
I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job!
ReplyDeleteSalesforce.com training in chennai
Salesforce crm Training in Chennai
Thank you :)
DeleteTOTAL LIFESAVER !
ReplyDeleteCool! Do you know how to get Authentication Status from the APEX ?
ReplyDeleteIn this example, do you connect salesforce with the same enviroment, but... how I can connect Salesforce to Salesforce using this protocol?
ReplyDelete