Crittercism Curl Script to Java HttpUrlConnection

360 Views Asked by At

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&[email protected]&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&[email protected]&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
3

There are 3 best solutions below

0
On BEST ANSWER

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!

1
On

[email protected] here, I wrote the docs you're reading and would love to help with this. I took a lot of care to return descriptive error messages from the API, if you could post those, it would be very helpful.

A few things to check:

  • WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo isn't a valid OAuth2 client id in our system (I just checked). Make sure you get an OAuth2 client id for your app. Currently we don't have an automated process for this (you have to email support) but it's on our roadmap.
  • In the cURL command, -u means "Basic Authorization"; you've got that part right. We currently support only public clients (not confidential) which means you don't need to use a password with our OAuth2 client id. However, since you're constructing the string by hand, the standard dictates that you have to put a colon between the username and password, even if there is no password. So for client id WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo, the final pre-encoding string would be "WV3v7ZTbYmqtUOMNvO7oPhLi8RN9zFoo:", which would encode to "V1YzdjdaVGJZbXF0VU9NTnZPN29QaExpOFJOOXpGb286" (as mentioned by Hans).

Let me know if either of these work.

0
On

the encoded string should contain the base64 encoded client_id as well as the client password, as in:

String encoded = new String(Base64.encodeBase64(new String(clientID + ":" + clientPassword)));