2

I'm trying to make a RESTful API call to a Parse.com server using HTTPURLConnection. Unfortunately, when I run the code below, I get an error of HTTP 401 (Unauthorised Access)

Here are the codes

protected JSONObject doInBackground(String... params) {
HttpsURLConnection urlConnection = null;
try {
  URL url = new URL("https://api.parse.com/1/functions/hello");
  urlConnection =
    (HttpsURLConnection) url.openConnection();

  urlConnection.setConnectTimeout(20000);
  urlConnection.setReadTimeout(20000);
  urlConnection.setDoOutput(true);
  urlConnection.setRequestMethod("POST");
  urlConnection.setRequestProperty("X-Parse-Application-Id", appId);
  urlConnection.setRequestProperty("X-Parse-REST-API-Key", restApiKey);
  urlConnection.setRequestProperty("Content-Type", "application/json");
  urlConnection.connect();

  byte[] outputBytes = "{}".getBytes("UTF-8");
  OutputStream out = urlConnection.getOutputStream();
  out.write(outputBytes);
  out.close();

  int statusCode = urlConnection.getResponseCode();
  if (statusCode != HttpURLConnection.HTTP_ACCEPTED) {
    Log.d(TAG, "doInBackground(): connection failed: statusCode: " + statusCode);
    //                    return null;
  }

  InputStream in = new BufferedInputStream(
      urlConnection.getInputStream());
  String responseText = getResponseText(in);
  Log.d(TAG, "doInBackground() responseText: " + responseText);
  return new JSONObject(responseText);

} catch (IOException | JSONException e) {
  e.printStackTrace();
} finally {
  if (urlConnection != null) {
    urlConnection.disconnect();
  }
}
return null;

Basically, all I need is to replicate the functionality of this cURL:

curl -X POST \
  -H "X-Parse-Application-Id: {APP_ID}" \
  -H "X-Parse-REST-API-Key: {API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{}' \
  https://api.parse.com/1/functions/hello

I ran the exact same cURL command with the same App ID and API key, and they are returning the correct result.

Any suggestions ??

EDIT:

Here is the console output I get:

09-24 14:28:43.101  21868-22057/com.example.versiontrack D/Module_VersionTracker﹕ doInBackground(): connection failed: statusCode: 401
09-24 14:28:43.102  21868-22057/com.example.versiontrack W/System.err﹕ java.io.FileNotFoundException: https://api.parse.com/1/functions/hello
09-24 14:28:43.102  21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
09-24 14:28:43.103  21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
09-24 14:28:43.103  21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at com.example.module_versiontracker.Module_VersionTracker$ParseVersionCheckTask.doInBackground(Module_VersionTracker.java:215)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at com.example.module_versiontracker.Module_VersionTracker$ParseVersionCheckTask.doInBackground(Module_VersionTracker.java:115)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-24 14:28:43.104  21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-24 14:28:43.105  21868-22057/com.example.versiontrack W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
5
  • HttpURLConnection.HTTP_ACCEPTED = 202, don't you want to look to 200 (HttpURLConnection.HTTP_OK) ? Btw your code seems good. 401 can only comes from the server. Check String urlReturned = httpsURLConnection.getURL().toString(); to see if the url is not modified. Commented Sep 24, 2015 at 6:54
  • The URL is the same. Commented Sep 24, 2015 at 7:27
  • So you can see the outputs of the request property with: Map<String, List<String>> map = httpURLConnection.getRequestProperties(); for (Map.Entry<String, List<String>> entry : map.entrySet()) { Log.d(TAG, "entry:" + entry.getKey()); for (String str : entry.getValue()) { Log.d(TAG, "value:" + str); } } and see if your authentication is correct. Commented Sep 24, 2015 at 7:43
  • So I got the same problem, have you got it working??? Commented Nov 6, 2015 at 17:38
  • I did. The code was working fine. The issue was that I mistakingly copy-pasted the API_KEY instead of the REST_API key from Parse. Commented Nov 8, 2015 at 1:04

1 Answer 1

2

I think you forgot to flush the output stream.

DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.write(outputBytes);
out.flush(); //Don't forgot to flush the output
out.close();
Sign up to request clarification or add additional context in comments.

1 Comment

No difference at all. Please check my edit for a console output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.