0

I am using a Graph Story neo4j server and I am using the REST API from my android app.

I using the URL like this https://uName:pass@host:port This same url and the http parameters and headers I set work in chrome Rest Client but I get a 401 status error from neo4j when I use my android app.

Calling this from my MainActivity:

            new RegisterRestTask().execute("https://GraphStoryUsername:GraphStoryPassword@GraphStoryNeo4jInstanceURL:7473/db/data/cypher");

This is my AsyncTask Code:

public class RegisterRestTask extends AsyncTask<String,Void,String> {

@Override
protected String doInBackground(String... urls) {


    HttpClient httpClient=new DefaultHttpClient();

    HttpPost httpPost=new HttpPost(urls[0]);


    String json="{ \"query\":\"Match n return n\"} ";

    try {
        JSONObject postJsonObject=new JSONObject(json);
        StringEntity jsonEntity=new StringEntity(postJsonObject.toString(),"UTF-8");
        httpPost.setHeader("Accept", "application/json; charset=UTF-8");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setEntity(jsonEntity);

        //httpPost.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Log.e("response",Integer.toString(httpResponse.getStatusLine().getStatusCode()));


    }
    catch (Exception e)
    {
        e.printStackTrace();
    }




    return null;
}

}

Chintan Shah
  • 935
  • 2
  • 9
  • 28

1 Answers1

1

401 typically would indicate that you need to pass credentials (as opposed to 403 which indicates the credentials were rejected).

Chrome takes the username/password parameters in the URL and passes them as HTTP Basic Auth. It does not send them in the clear. This is a special client feature (that exists on many clients). You need to set your Android apps' HTTP request to use basic auth.

If you show your Android code, someone (maybe myself) could help you on what changes to make.

Ryan Boyd
  • 2,978
  • 1
  • 21
  • 19
  • See this for passing the username/password as an additional Authentication header: http://stackoverflow.com/questions/12443050/android-httpclient-json-post-with-basic-authentication – Ryan Boyd Feb 24 '15 at 03:38