You need to sign in to do that
Don't have an account?
Austin Villanueva
REST API authentication with username password NEED HELP ASAP
I try it with curl and it works but I don't know why it doesn't work on my workplace
curl -v https://login.salesforce.com/services/oauth2/token -d "grant_type=password&client_id={clientid}&client_secret={clientsecret}&username={emailadd}&password={password+securityToken}" -H "Content-Type:application/x-www-form-urlencoded"
I also try it in my own laptop and it works. That's what I don't understand. I used the same jar file for httpclient for both pc and in my laptop it works but not on my work place. Is there any other reason why it doesn't work this is the sample code
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
String authenticationUrl = "https://login.salesforce.com/services/oauth2/token";
// authentication
HttpPost requestPost = new HttpPost(authenticationUrl);
requestPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
requestPost.setHeader("Accept", "application/json");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", client_id));
params.add(new BasicNameValuePair("client_secret", client_secret));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password+securityToken));
requestPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(requestPost, null).get();
System.out.println("Status code:" + response.getStatusLine().getStatusCode());
System.out.println("Entity: " + response.getEntity().getContent().toString());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
curl -v https://login.salesforce.com/services/oauth2/token -d "grant_type=password&client_id={clientid}&client_secret={clientsecret}&username={emailadd}&password={password+securityToken}" -H "Content-Type:application/x-www-form-urlencoded"
I also try it in my own laptop and it works. That's what I don't understand. I used the same jar file for httpclient for both pc and in my laptop it works but not on my work place. Is there any other reason why it doesn't work this is the sample code
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
String authenticationUrl = "https://login.salesforce.com/services/oauth2/token";
// authentication
HttpPost requestPost = new HttpPost(authenticationUrl);
requestPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
requestPost.setHeader("Accept", "application/json");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("client_id", client_id));
params.add(new BasicNameValuePair("client_secret", client_secret));
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password+securityToken));
requestPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(requestPost, null).get();
System.out.println("Status code:" + response.getStatusLine().getStatusCode());
System.out.println("Entity: " + response.getEntity().getContent().toString());
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
Can you check if your workplace is using any Proxy settings? The easiest way to find is to check the workplace browser settings.
If Yes, check on how to use curl using proxy. Hope this helps.
Good Luck,
Sridhar