fbpx

Free Salesforce Developers Tutorials >

Chapter 13 - API >

REST API Part 3

REST API Part 3

What You’ll Learn


S2 Labs

Authorizing Salesforce From Postman

Set up OAuth 2.0:

In Salesforce, from Setup, enter Apps in the Quick Find box, then select Apps, and under Connected Apps click New to create a new connected app. 

We have to use this connected app because Apex REST Service inside salesforce cannot be accessed directly so it needs a link to be connected we have to use the connected app 

  • Give the connected app any name.
  • Check on the Enable OAuth Settings.
  • The Callback URL you supply here is the same as your Web application’s callback URL. In this case, as we are sending a request through Postman the callback URL must be of Postman. Give this URL, https://www.getpostman.com/oauth2/callback, as a callback URL.
  • In the OAuth scope give it full access and click on save.

As you click on save you will get two more parameters that are used in this process of authentication that is Consumer Key and Consumer Secret. 

Generating Access Token

Now In Postman, we have to generate an access token that can be used for calling out the service. Access token defines that we have used proper credentials to login into the account and we can proceed further.

So to generate an access token follow these steps:

  1. Open Postman
  2. Set Method to Post 
  3. Set the method URL to https://login.salesforce.com/services/oauth2/token
  4. In Body define these parameters
  • grant_type = password 
  • client_id = Consumer Key 
  • client_secret = Consumer Secret 
  • username = your Salesforce Account username 
  • password = password+security_token 

After applying these steps click on Send button and as a result, an Access token and your instance URL is generated so save it for future reference. 

Calling Salesforce API Using Access Token

The access token that is generated is used in REST Request to call the web service so to call the service the URL is the same as the URL mapping of Apex-Service for example: “https://instance_name.salesforce.com/services/apexrest/URLMapping”

Provide your instance name and use this URL to reach out to your APEX REST Service.

Now in the header part provide the access token and content type. 

Authorization: Bearer “your access token” Content-type: application/json
Now we have to pass our JSON data that is used to create leads in salesforce. Save your JSON data in a JSON format file named Lead.json. The data is something like 

{“First Name”: “testing”, “Last Name”: “Leads”, “Company”: “XYZ Company”}
Click on the binary button in the Body column and choose the file you want to send as JSON data and then click on send button.
Now you can go and check in the leads tab that your lead is created. Similarly, you can use this approach for any object and with more data. 

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *