Saturday 8 September 2018

Salesforce Authentication Using Rest API - Connected App

Arpit Sharma

In Salesforce, REST API uses bearer token authentication to get into Salesforce and perform operation.

Below are the steps that shows how Rest API works
1. Request for Bearer token using connected app configuration and Salesforce credentials.
2. Salesforce response with bearer token.
3. Use that bearer token to get into Salesforce and perform operation.

In this post I will let you know how you can get bearer token with the help of Connected app and credentials. 

Create Connected app 
1.  Go to Setup -> Quick Find -> Apps -> Connected Apps -> Manage Connected Apps

 2. Click on New Connected App

 3. I configure my connected app as below screenshot,you can set up as your requirement.
 Once you click on Save you will get below screen.
I have use Consumerkey and Consumer secret from above connected app configuration and salesforce username and password to connect with Salesforce in my java application.

You can connected with Salesforce by using any other application as per your convenience.

Below is Java code that I have created to get bearer token with Salesforce.

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class SalesforceRestLogin {
 public static void main(String args[]) {
  try {
   HttpClient httpClientObj = HttpClients.createDefault();
   //end point URL
   HttpPost httpPostRequest = new HttpPost("");
   //set post body
   List<NameValuePair> requestBody = new ArrayList<NameValuePair>(2);
   //set grant type
   requestBody.add(new BasicNameValuePair("grant_type", "password"));
   //set connected app consumer key
   requestBody.add(new BasicNameValuePair("client_id", "connected app consumer key"));
   //set connected app client secret
   requestBody.add(new BasicNameValuePair("client_secret", "connected app client secret"));
   //set salesforce username
   requestBody.add(new BasicNameValuePair("username", "salesforce username"));
   //set salesforce password+security token
   requestBody.add(new BasicNameValuePair("password", "salesforce password+security token"));
   httpPostRequest.setEntity(new UrlEncodedFormEntity(requestBody, "UTF-8"));
   //response from server
   HttpResponse serverResponseObj = httpClientObj.execute(httpPostRequest);
   BufferedReader bufferReader = new BufferedReader(
     new InputStreamReader(serverResponseObj.getEntity().getContent()));
   StringBuffer responseRes = new StringBuffer();
   String line = "";
   while ((line = bufferReader.readLine()) != null) {
  }catch(Exception e) {
When you run above code then you will get server URL and session id in response, then by using both you can communicate with server for further requests.
Thank you for to come on my blog.
Let me know using comments if you like my article and for suggestions.