JChap
JChap

Reputation: 1441

Java HTTPS (SSL) Connection Return 400 Status where as CURL/ARC works for same URL

I'm trying to connect to salesforce production account via OAuth and exchange an access token by giving a refresh token.

When i make a call using Chrome Advanced Rest Client or CURL i'm able to get the access token; but when i'm trying to make the same call using java HttpClient or URLConnection i'm getting a 400 status.

    String url = "https://login.salesforce.com/services/oauth2/token";
    String requestBody = "grant_type=refresh_token&client_id=myClientId&client_secret=myClientSecret&refresh_token=myRefreshToken";

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "myapp/1.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(requestBody);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + requestBody);
    System.out.println("Response Code : " + responseCode);

    BufferedReader br = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = br.readLine()) != null) {
        response.append(inputLine);
    }
    br.close();

    //print result
    System.out.println(response.toString());

I examined both requests using wireshark. And both the requests look identical. Since the URL is https, i posted the request to a dummy URL and verified using wireshark.

Anybody ran into this kinda of issue ? BTW the same code works for developer edition and is not working for production edition. And for production edition it works with CURL and Chrome ARC.

Upvotes: 0

Views: 444

Answers (1)

JChap
JChap

Reputation: 1441

Salesforce only allows HTTPS connections on TLS 1.1 or higher. Since i'm running on Java 7, default is TLS 1.0. As a result the request is failing from Java.

By adding the following property we can add support for TLSv1.1 and TLSv1.2

-Dhttps.protocols=TLSv1.1,TLSv1.2

Upvotes: 1

Related Questions