You need to sign in to do that
Don't have an account?
Tanuj Tyagi
Salesforce REST API using Java
Hi All,
I am trying to make Connect Salesforce with Java using Rest Api. But I am getting authentication Error and status = 400
I followed the all the steps mentioned here :
http://resources.docs.salesforce.com/204/17/en-us/sfdc/pdf/salesforce_developer_environment_tipsheet.pdf
Below is my Java Code:
public class Main {
static final String USERNAME = "tanujsfdcfsp@gmail.com";
static final String PASSWORD = "******************";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
static final String CLIENTID = "***********";
static final String CLIENTSECRET = "********************";
public static void main(String[] args) {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 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);
/// HttpGet httpGet = new HttpGet(loginURL);
HttpResponse response = null;
try {
// Execute the login POST request
response = httpclient.execute(httpPost);
} catch (ClientProtocolException cpException) {
System.out.println("Exception: "+cpException);
// Handle protocol exception
} catch (IOException ioException) {
// Handle system IO exception
}
// 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) {
// Handle system IO exception
}
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) {
// Handle JSON exception
}
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 trying to make Connect Salesforce with Java using Rest Api. But I am getting authentication Error and status = 400
I followed the all the steps mentioned here :
http://resources.docs.salesforce.com/204/17/en-us/sfdc/pdf/salesforce_developer_environment_tipsheet.pdf
Below is my Java Code:
public class Main {
static final String USERNAME = "tanujsfdcfsp@gmail.com";
static final String PASSWORD = "******************";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
static final String CLIENTID = "***********";
static final String CLIENTSECRET = "********************";
public static void main(String[] args) {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 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);
/// HttpGet httpGet = new HttpGet(loginURL);
HttpResponse response = null;
try {
// Execute the login POST request
response = httpclient.execute(httpPost);
} catch (ClientProtocolException cpException) {
System.out.println("Exception: "+cpException);
// Handle protocol exception
} catch (IOException ioException) {
// Handle system IO exception
}
// 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) {
// Handle system IO exception
}
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) {
// Handle JSON exception
}
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();
}
}
When you are connecting to Salesforce through REST API, here are a couple of things you need to check when you are getting the above error.
- The profile of the user you are connecting to has ‘API Enabled’ permission (Setup -> Users -> Profile -> Profile Name -> System Permissions)
- You have given the correct ‘OAuth Scope’ when creating Connected Apps. If in doubt, select ‘Full Access (full)’ OAuth scope and try (Setup -> Create -> Apps -> Connected App Name)
- If the ‘Permitted Users’ under ‘OAuth Policies’ is defined as ‘Admin approved users are pre-authorized’, you have added the profile of the user with which you are trying to connect to the ‘Profiles’ under that ‘Connected App’ (Setup -> Create -> Apps -> Connected App Name -> Manage -> Profiles)
- ‘IP Relaxation’ is set to ‘Relax IP restrictions for the Connect App (Setup -> Create -> Apps -> Connected App Name -> Manage)
- If you have NOT specified Login IP Ranges under Setup -> Manage Users -> Profiles -> Profile Name -> Login IP Ranges, then you are appending security token to your password when connecting through REST API
- Your password does not contain characters like ‘&’
Still, if the problem persists give a try by removing all “amp;” from Note: Also, if your email address has plus symbol in it (e.g. http://myname+salesforce-signup@gmail.com) then you need to replace the + with %2b (e.g. http://myname%2bsalesforce-signup@gmail.com)Regards,
Nagendra.