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
Austin VillanuevaAustin 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);
 
Sridhar NarayansaSridhar Narayansa
Hey Austin,

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

 
Austin VillanuevaAustin Villanueva
Hey Sridhar, Thanks for the respond. I already check it. We are not using any proxy here in my work. And I try to changed my httpclient library to okhttpclient and it works fine with my computer but I still get the same result {"error":"unknown_error","error_description":"retry your request"} I don't know why. but we are using a static ip. Also, I already did an http request to other api call from different services and there's no problem. That's what I really don't know why it doesn't work. I hope you can help me about this problem. Thanks. Sincerely, Austin