javacurlcrittercism

Crittercism Curl Script to Java HttpUrlConnection


I have been trying to convert the following Curl script to Java in order to get the acces token, but seems to fail all the time with either a 400 or 401. What am I doing wrong? Is the curl conversion to java rightly done below? Please advice...

Curl Script:

curl -X POST https://developers.crittercism.com/v1.0/token -u WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo -d 'grant_type=password&username=david@crittercism.com&password=riTiBw28%3DpmFu'

Where, "WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo" is the oAuth ClientID.

Java Code:

    String url = "https://developers.crittercism.com/v1.0/token";
    String urlParameters = "grant_type=password&username=myemail@email.com&password=secretpass";
    String clientId = "nD8FiwFiOB1rpU5HVc2ESyRAwbFOOO:";  //Just for ref

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

    //add request header
    String basicAuth = new String(Base64.encode(clientId.getBytes()));
    con.setRequestProperty ("Authorization", String.format("Basic %s", basicAuth));
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestProperty("Accept", "*/*");
    con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
    con.setRequestMethod("POST");

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

The exception that is being thrown,

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://developers.crittercism.com/v1.0/token
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at CrittercismClient.sendPost(CrittercismClient.java:102)
at CrittercismClient.main(CrittercismClient.java:26)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://developers.crittercism.com/v1.0/token
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at CrittercismClient.sendPost(CrittercismClient.java:96)
... 1 more

Solution

  • It's working now!! Figured out that the colon is not required after the clientID, removing that made it to work. But I bet what really made it to work, is probably the extra request header properties that I have added in. Anyways, thanks for your time people!