Skip to main content

Rest Integration Test using Postmaster - OAuth 2.0

Hi ,

So you have created a Rest class and exposed it. At this stage you know how you can test this using Workbench.

Ex: If your class name is : getAccountDetails which is expecting 2 parameters.

then in the workbench, you will go to rest Explorer and select Get/Post based on the requirement and paste the URL something similar to this.

/services/apexrest/CKDomain/getAccountDetails?Param1=1100055515&Param2=13706195

And BAM!!!! Everything looks fine.

But in real time scenarios, you might want to give this endpoint URL to some other system (Say SAP/Oracle) and you want to test from your end before informing them about the endpoint URL. So how can we achieve this.

There are many ways to test this and I prefer using Postman app for this. Install Postman from here.

Now , In order to successfully receive a Rest call out from Other system to Salesforce , there are multiple auth techniques. I would like to use the most commonly used auth technique - OAuth 2.0

As per my knowledge, a Simple OAuth 2.0 authentication needs 4 important parameters and they are,
1.User Name
2.Password
3.Client Secrete
4.Client ID

NOTE: If you are trying make a webservice callout another salesforce instance then please follow this post

How do we get the Client Secret and Client ID ? To get this unique gate pass informations we need to create a connected app.

Go to Setting >>App setup >>  Apps >>Scrolldown to bottom >>Connected Apps >>Click new and fill in the details as shown in the below diagram.

Now, it may take a while for salesforce to establish this connect app. After 2- 10 mins you can go to the connected app and you will see something similar in the below image.
Now we have Consumer Key /Client ID and Client Secrete.

Let go to Postman app Now.

After launching the app you can click on New tab and select 'POST'. In the same click on the Params button. In the Authorization sub tab select OAuth 2.0

Now you can see something like below image.


Now in the Postman URL section you we need to enter the endpoint URL which other systems would be hitting in order to get into salesforce system.

Step 1. First one should make a call out to get the access_token
Step 2. Make a actual callout for getting the desired data with access_token obtained in the previous step.

Step 1 :
In Postman's URL section enter :

https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=***Consumer Key from connected App **&client_secret=**Client Secrete ID from connected App**&username=chanz707@gmail.com&passwor=**your org password+securityToken**

If you have IP Restrictions ON, then no need of providing the Security Token info along with the password.
After you enter all the detail correctly the request would look similar to below image.



Now you are a authorized third party system.

Step 2:
Once you have the access_token, now its time to hit the actual end point to get the desired data with obtained access_token. Another important parameter to be noticed is instance_url . Till now you used login.salesforce.com / test.salesforce.com .

Now the next call out should include the instance_url and access_token from previous step.

Next call out will look like below image with 2 header information as shown.
Authorization : Bearer + access_token
content_type : application/json

Sample Endpoint URL :

https://***instance_url***/services/apexrest/getProductDetailsSmartSearch?searchString=VNXe&userBadgeId=1100055515



Thats All, You have tested yourself that third party system can access the exposed data using Rest endpoint URL










Comments

Post a Comment

Popular posts from this blog

Salesforce - Invoke Apex from Visual Flows

This post will brief you about how one can invoke an Apex class from Visual Flows. Important Note: The method which you would like to invoke should be marked as  @InvocableMethod  and the variables which you would like use in and out of Flow, we need to mark them as  @InvocableVariable Best Practices : Make 2 wrapper classes. One for getting the values from Flow to Apex and other wrapper class is to get the values from Apex to Flow. The below sample code explains how you can get a detail in and out of the flow. Use Case : We ask user to enter the Account ID which they want to update and New Name which they want to update on the entered Account ID. Note: Since this is for the demo purpose, we are asking user to enter the Account ID (SFDC 15/18 digit ID ). In Ideal scenario we can invoke the Flow from custom button/links. (  Refer This ) Here we created a Small Visual Flow which has 2 screens ( 1. For asking details from user and other for Showing the success/fa

Salesforce to Salesforce Integration using OAuth 2.0 and Named Credentials

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