You need to sign in to do that
Don't have an account?
Avinash Barola
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
public class OAuthConnectedApp {
static final String USERNAME = "avinash.barola@metacube.com";
static final String PASSWORD = "9782700490afo3GKfhFClkBKYoHAhtCTiQbs";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
static final String CLIENTID = "3MVG9ZL0ppGP5UrCJNszgnq4XwZ06OLxPHU3_A_EOVLtF0IOuvGzzq3hldpuBTAYp.kfNfqh2_PFVDMtGISGi";
static final String CLIENTSECRET = "3825749946662741519";
public static void main(String[] args) {
HttpClient httpclient = HttpClientBuilder.create().build();
// Assemble the login request URL
String loginURL = LOGINURL +
GRANTSERVICE +
"&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) {
cpException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
// verify response is HTTP OK
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Error authenticating to Force.com: "+statusCode);
// Error is in EntityUtils.toString(response.getEntity())
return;
}
String getResult = null;
try {
getResult = EntityUtils.toString(response.getEntity());
} catch (IOException ioException) {
ioException.printStackTrace();
}
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");
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
System.out.println(response.getStatusLine());
System.out.println("Successful login");
System.out.println(" instance URL: "+loginInstanceUrl);
System.out.println(" access token/session ID: "+loginAccessToken);
// release connection
httpPost.releaseConnection();
}
}
I am getting Error authenticating to Force.com: 400
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
public class OAuthConnectedApp {
static final String USERNAME = "avinash.barola@metacube.com";
static final String PASSWORD = "9782700490afo3GKfhFClkBKYoHAhtCTiQbs";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
static final String CLIENTID = "3MVG9ZL0ppGP5UrCJNszgnq4XwZ06OLxPHU3_A_EOVLtF0IOuvGzzq3hldpuBTAYp.kfNfqh2_PFVDMtGISGi";
static final String CLIENTSECRET = "3825749946662741519";
public static void main(String[] args) {
HttpClient httpclient = HttpClientBuilder.create().build();
// Assemble the login request URL
String loginURL = LOGINURL +
GRANTSERVICE +
"&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) {
cpException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
// verify response is HTTP OK
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Error authenticating to Force.com: "+statusCode);
// Error is in EntityUtils.toString(response.getEntity())
return;
}
String getResult = null;
try {
getResult = EntityUtils.toString(response.getEntity());
} catch (IOException ioException) {
ioException.printStackTrace();
}
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");
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
System.out.println(response.getStatusLine());
System.out.println("Successful login");
System.out.println(" instance URL: "+loginInstanceUrl);
System.out.println(" access token/session ID: "+loginAccessToken);
// release connection
httpPost.releaseConnection();
}
}
@ Adrian : i think he has passed the credential with the authentication seemes to be
"static final String PASSWORD = "9782700490afo3GKfhFClkBKYoHAhtCTiQbs";"
even i did the same way can you advice us how to pass it ?