[SalesForce] Error 500 java rest client , but works on postman

I am using the same code mentioned in https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/code_sample_auth_oauth.htm
It works fine in Chrome extension POSTMAN. I am passing the same POST parameters that is used in POSTMAN in my java code. Any help is appreciated.

I am getting 500 error with below response

An internal server error has occurred

An internal server error has occurred

An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.

Thank you again for your patience and assistance. And thanks for using salesforce.com!

An internal server error has occurred

An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.

Thank you again for your patience and assistance. And thanks for using salesforce.com!

Error ID: 1328242241-114053 (801801977)

public static void oAuthSessionProvider(String loginHost, String username, String password, String clientId, String secret)
        throws HttpException, IOException
{
    // Set up an HTTP client that makes a connection to REST API.
    DefaultHttpClient client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    HttpClientParams.setCookiePolicy(params, CookiePolicy.RFC_2109);
    params.setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 30000);

    // Set the SID.
    System.out.println("Logging in as " + username + " in environment " + loginHost);
    String baseUrl = loginHost + "/services/oauth2/token";
    // Send a post request to the OAuth URL.
    HttpPost oauthPost = new HttpPost(baseUrl);
    // The request body must contain these 5 values.
    List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();
    parametersBody.add(new BasicNameValuePair("grant_type", "password"));
    parametersBody.add(new BasicNameValuePair("username", username));
    parametersBody.add(new BasicNameValuePair("password", password));
    parametersBody.add(new BasicNameValuePair("client_id", clientId));
    parametersBody.add(new BasicNameValuePair("client_secret", secret));
    oauthPost.setEntity(new UrlEncodedFormEntity(parametersBody, HTTP.UTF_8));

    // Execute the request.
    System.out.println("POST " + baseUrl + "...\n");
    HttpResponse response = client.execute(oauthPost);
    int code = response.getStatusLine().getStatusCode();
    System.out.println(code);
    System.out.println(response.getStatusLine().getStatusCode());
    String theString = IOUtils.toString((
            new InputStreamReader(response.getEntity().getContent())));
    System.out.println("response-->"+theString);
}

Best Answer

Hi Thank you everyone for your help. I was able to resolve the issue by upgrading to Java 8. The underlying cause was that, salesforce discontinued support for TLS1.0. Java 7 by default is not TLS1.1/1.2 but Java 8 is. The 500 error misguides the user. This is something Salesforce need to update with correct error message. The error message appears correctly when testing web-server authorization.

I found the solution accidentally while trying to implement the "web-server" authorization method . Salesforce was responding with 400 error message TLS 1.1 or higher required and I solved that by upgrading java.

Related Topic