Skip to main content

Salesforce REST API - Part II

We have created a Connected App in Part I , so now we need to make requests through the rest api.

So as I mentioned earlier we need grant_type , client_id , client_secret , username , password parameters to authenticate with the salesforce in order to call to the endpoint https://login.salesforce.com/services/oauth2/token. And that request should be a POST request.

What will happen here is once we send that POST request with the parameters if it is a successful authentication it will return a access token.This access token is same as the session id. After that each and every request we need to attach the access token in the header. Behind the seen salesforce validate the access token and if it is validate it will returning the request data. if the validate fails salesforce returning 401 unauthorized error. 

As you know session has expire time. As same as salesforce access token has expire time.You can change access token settings by clicking the Connected App's manage button then the edit button. After that you will be see below screen.


In here you can customize the Refresh Token Policy and it including the refresh token time out and many more.
 
If the access token expires , then that access token is no more valid , so we need to request for a refresh token via OAuth end point along with the old access token/refresh token.

Once you authenticate with the salesforce you will be able to do lots of things via the rest api. As a example we can call GET , POST , PUT, PATCH , DELETE , HEAD http methods on the rest ap. Below I mentioned some get services exposed in saleforce REST API.




So Now I will show you how to call for a salesforce rest api through the post man chrome extension. Here I choose post man because it is really easy to test rest api's. There are many more great tools to make the requests.

  • Once you download then go the postman page by clicking post man extension. And paste below URL in the place holder called Enter request URL here and set http method to POST.
  • Then click URL params button and add following parameters.   
            grant_type  :  password
            client_id : 'Connected app's Consumer Key'
            client_secret : 'Connected app's Consumer Secreat'
            username : 'your salesforce username'
            password : 'your salesforce password + security token'

  • Please see below screen to get client_id and client_secret. You need to get security token by  clicking My settings  -> Personal -> Reset Security Token. This will send the security token by email.
 
  • Now you got everything to call to salesforce OAuth endpoint. So you need to set all parameters as below screen in post man. 

  • So click send , If the endpoint name and method , parameters are correct you will receive an access token.


  • Common Issues : 
  1. When you coping client-secret or client_id you will copy the text with some spaces. So make sure
    client-secret or client_id has no spaces.
  2. Didn't append security token with the password. Make sure you have paste security token after the password in password parameter.
  3. Didn't change the http method to POST. Make sure you are sending a POST http method
  •  I will explain how to get salesforce data through the rest call in the Part III

Comments

Popular posts from this blog

Salesforce Metadata API

In this post I'm going the cover basics in the Metadata API and how it can connect with a .net console application. Then after I will show you how to create a custom object and create fields by using the metadata api from the console app. What is Salesforce Metadata API There are many things can be done from the metadata api. you can create Objects,Fields,Validation,Workflows, Profiles, Reports for a particular client organization just by running the piece of code so that you need not do all the customization settings manually which will consume time if you have to do that several times for different clients for the same configuration settings. You can also take the backup of your organization customization settings just by fetching the metadata WSDL from your Salesforce org. You can read, create, update, delete following components in a Salesforce organization. Custom Objects/Fields. Visualforce pages. Page Layouts. Validation rules. Apex. Workflows. Approval processe

Exploring the Marvels of Salesforce Marketing Cloud: Unleashing the Power of Marketing Automations

Salesforce Marketing Cloud is a comprehensive marketing automation platform offered by Salesforce, a leading customer relationship management (CRM) company. It is designed to help businesses manage and optimize their marketing efforts across various channels and touchpoints. The platform enables organizations to create, execute, and analyze marketing campaigns to engage with their target audiences more effectively and drive better results. Key features and capabilities of Salesforce Marketing Cloud include: Email Marketing: Users can create and send personalized email campaigns to segmented audiences, track email performance metrics, and automate email workflows to nurture leads and build customer relationships. Journey Builder: This tool allows marketers to design and automate customer journeys across multiple channels such as email, mobile, social media, and advertising. It helps create personalized experiences based on customer behavior and interactions. Social Media Marketing: Sale

Salesforce REST API

This post describe about the salesforce rest api and using salesforce rest api . Salesforce implements the OAuth to authenticate soap and rest calls with the client. So I will describe with the simple steps, how to create salesforce app that expose outside to authenticate and call rest methods. First you need to create a salesforce developer account if you don't have an developer account or sandbox account. If you don't have an developer account go with the following link and create an account. It's free.   https://developer.salesforce.com/signup . Once you create the developer account go to https://login.salesforce.com/ and login with your credentials.     Navigate to setup and type app and you will be able to find Apps section and click the link. So now you will be able to find Connected Apps section and click New. What we doing here is creating a connected app and that app will exposing to outside to authenticate and call rest api calls. So you can insert C