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

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...

Integrate .NET console application with Salesforce Rest API

According to the integration perspective in Salesforce there are several APIs to integrate with external systems. As an example there are REST API, SOAP API, Bulk API, Tooling API and Streaming API. Here I will focusing for Rest API. I have created a different post which will describing the integration with Salesforce Soap API. You can find it from  here . And for the Tooling API you can find it from  here . 1st of all you need to create a connected app in Salesforce in order to make the bridge to integrate with console app.  A connected app integrates an external application with Salesforce using APIs. Connected apps use standard SAML and OAuth protocols to authenticate, provide single sign-on, and provide tokens for use with Salesforce APIs. So I have created a separate post which will describe how to create a connected app to get consumer id and consumer secret. Please follow  this   post to get consumer id and consumer secret. After you...

How to automate testing using Selenium and C#

Hi All, today I’m going to explain about Selenium which is a open source automation tool and how to utilize it with C#. As you know automation tools save our time in terms of testing a software product. If we are doing some regression testings automation tools doing a great job in terms of saving the time. So I’m going to explain a very simple example for using Selenium from C#. We can divide selenium as two entities. One is Selenium IDE as a web browser extension. And the other one is Selenium Web Driver. So I’m going to talk about the selenium web driver which is using for web based testings from C# code. Basically Selenium Web driver will open the browser and doing the actions as we instructing in the C#code. Firstly you need to install visual studio. For my case I have installed visual studio community version which is a free version of Microsoft. Then you need to create a project by navigating to File -> New -> Project. And give a name for the project and click ok...