function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Website AdminWebsite Admin 

Can you get an oauth token without a browser?

I have Salesforce enterprise and would like to automate some functionality of pushing data from our main system into salesforce.  The issue I’m running into is getting an auth token.  I can follow the pattern with a browser and it works as expected.  The issue is that I don’t want to use a browser.  I want my Java server to make an http call and get a call back with the token.  The reason it does not work is that salesforce uses javascript redirects and cookies instead of 301 and http redirects.  

I’ve seen some mention of getting a session token and then performing the operation but that will not work due to the lack of a javascript engine in my http client.  Are there any alternatives in getting this to work?
Yes there are several flows you can follow to login to Salesforce from an external application. You can either use the SOAP login call or OAUTH.


Here's a good overview of OAuth:

An autonomous client can obtain an access token by simply providing username, password and (depending on configuration) security token in an access token request. Again the request is POSTed (1) to, but the payload now has the form


The following parameters are required:

grant_type Set this to password.
client_id Your application's client identifier.
client_secret Your application's client secret.
username The API user's username, of the form
password The API user's password. If the client's IP address has not been whitelisted in your org, you must concatenate the security token with the password.
Tanuj TyagiTanuj Tyagi
Hi  Website Admin,
To make an http call and get a call back with the token , you need to make a Connected App in your instance and get CLIENTID  and CLIENTSECRET  from the connectd App.

Use this Code
Hope it will help.

public class RestApiTest {

 static final String USERNAME = "xxxxxxxxxxxxxxxxxxx";
 static final String PASSWORD = "xxxxxxxxxxxxxxxxxxx";
 static final String LOGINURL = "";
 static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
 static final String CLIENTID = "xxxxxxxxxxxxxxxxxxx";
 static final String CLIENTSECRET = "xxxxxxxxxxxxxxxxxxx";
 private static String REST_ENDPOINT = "/services/data";
 private static String API_VERSION = "/v32.0";
 private static String baseUri;
 private static Header oauthHeader;
 private static Header prettyPrintHeader = new BasicHeader("X-PrettyPrint", "1");
 private static String leadId;
 private static String leadFirstName;
 private static String leadLastName;
 private static String leadCompany;

 public static void main(String[] args) {

  HttpClient httpclient = HttpClientBuilder.create().build();

  // Assemble the login request URL
  String loginURL = LOGINURL +
   "&client_id=" + CLIENTID +
   "&client_secret=" + CLIENTSECRET +
   "&username=" + USERNAME +
   "&password=" + PASSWORD;

  // Login requests must be POSTs
  HttpPost httpPost = new HttpPost(loginURL);
  HttpResponse response = null;

  try {
   // Execute the login POST request
   response = httpclient.execute(httpPost);
  } catch (ClientProtocolException cpException) {
  } catch (IOException ioException) {

  // verify response is HTTP OK
  final int statusCode = response.getStatusLine().getStatusCode();
  if (statusCode != HttpStatus.SC_OK) {
   System.out.println("Error authenticating to " + statusCode);
   // Error is in EntityUtils.toString(response.getEntity())

  String getResult = null;
  try {
   getResult = EntityUtils.toString(response.getEntity());
   System.out.println("Result From Server: "+getResult);
  } catch (IOException ioException) {
  JSONObject jsonObject = null;
  String loginAccessToken = null;
  String loginInstanceUrl = null;
  try {
   jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
   loginAccessToken = jsonObject.getString("access_token");
   loginInstanceUrl = jsonObject.getString("instance_url");
   System.out.println("loginAccessToken: "+loginAccessToken+" "+"loginInstanceUrl: "+loginInstanceUrl);
  } catch (JSONException jsonException) {
  baseUri = loginInstanceUrl + REST_ENDPOINT + API_VERSION;
  oauthHeader = new BasicHeader("Authorization", "OAuth " + loginAccessToken);
  System.out.println("oauthHeader1: " + oauthHeader);
  System.out.println("Successful login");
  System.out.println("  instance URL: " + loginInstanceUrl);
  System.out.println("  access token/session ID: " + loginAccessToken);

  System.out.println("baseUri: " + baseUri);

  // Run codes to query, insert, update and delete records in Salesforce using REST API
 // createLeads();
 // deleteLeads(); 

  // release connection